﻿using APICommond;
using BusinessEntity;
using BusinessEntity.Assets;
using BusinessEntity.HTGLQueery;
using BusinessEntity.QuerryEntity;
using BusinessEntity.Warning;
using BusinessRepository;
using BusinessRule.Organization;
using Newtonsoft.Json.Linq;
using NLog;
using NPOI.HSSF.Record;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule.Warning
{
    public class Waring
    {
        public static async Task<DataTable> GetFileZFPZ(string kjqj, string org, string where)
        {

            string sql = "select * from ZFPZ where ISSUE='" + kjqj + "' and AREACODE like '" + org + "%'";
            if (where != "")
            {
                sql += " and " + where;
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #region “合同导入接口”
        public static string GetString(JObject jb, string str)
        {
            string value = "";
            if (jb.Property(str) != null)
            {
                value = jb[str].ToString();
            }
            return value;
        }
        public static string GetDecimal(JObject jb, string str)
        {
            string value = "0";
            if (jb.Property(str) != null)
            {
                value = jb[str].ToString();
            }
            return value;
        }
        public static async Task<string> InsertContract(JObject jb)
        {
            string ver = "";

            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("begin ");


                sb.Append(@"delete from Contract where projcode='" + GetString(jb, "projcode") + "';" +
                    @"delete from Assets where projcode='" + GetString(jb, "projcode") + @"'; 
                                delete from ContractFile where projcode='" + GetString(jb, "projcode") + @"';  INSERT INTO Contract
           (projcode,areacode,contractfrom,contractcode,contractname,partya,partyb,contractstartdate
           ,contractenddate,contractcreatedate,ttlamt,firstrent,acctname,acctnumb,accbankname,mobilephone
           ,dutyperson,contractperson,contactmobile,auctionusertype,auctionusernumber,bank,bankaccount
           ,auctiondate,operatorname,operatorcardid,operatormobile) VALUES (");
                sb.Append("'" + GetString(jb, "projcode") + "','" + GetString(jb, "areacode") + "','" + GetString(jb, "contractfrom") + "',");
                sb.Append("'" + GetString(jb, "contractcode") + "','" + GetString(jb, "contractname") + "','" + GetString(jb, "partya") + "','" + GetString(jb, "partyb") + "',");
                sb.Append("'" + GetString(jb, "contractstartdate") + "','" + GetString(jb, "contractenddate") + "','" + GetString(jb, "contractcreatedate") + "',");
                sb.Append("'" + Convert.ToDecimal(GetDecimal(jb, "ttlamt")) / 10000 + "','" + Convert.ToDecimal(GetDecimal(jb, "firstrent")) / 10000 + "','" + GetString(jb, "acctname") + "',");
                sb.Append("'" + GetString(jb, "acctnumb") + "','" + GetString(jb, "accbankname") + "','" + GetString(jb, "mobilephone") + "','" + GetString(jb, "dutyperson") + "',");
                sb.Append("'" + GetString(jb, "contractperson") + "','" + GetString(jb, "contactmobile") + "','" + GetString(jb, "auctionusertype") + "','" + GetString(jb, "auctionusernumber") + "',");
                sb.Append("'" + GetString(jb, "bank") + "','" + GetString(jb, "bankaccount") + "','" + GetString(jb, "auctiondate") + "',");
                sb.Append("'" + GetString(jb, "operatorname") + "','" + GetString(jb, "operatorcardid") + "','" + GetString(jb, "operatormobile") + "');");

                string assets = GetString(jb, "assets");

                JArray jassets = JArray.Parse(assets);
                foreach (JObject jo in jassets)
                {
                    sb.Append("insert into Assets(projcode,assetcode,assetclass,assettransprice,priceunit,tradearea,tradeunit,assetid) values (");
                    sb.Append("'" + GetString(jb, "projcode") + "','" + GetString(jo, "assetcode") + "','" + GetString(jo, "assetclass") + "',");
                    sb.Append("'" + Convert.ToDecimal(GetDecimal(jo, "assettransprice")) / 10000 + "','" + GetString(jo, "priceunit") + "','" + GetDecimal(jo, "tradearea") + "',");
                    sb.Append("'" + GetString(jo, "tradeunit") + "','" + GetString(jo, "assetid") + "');");

                    string attachments1 = GetString(jo, "attachments");
                    if (attachments1 != "")
                    {
                        JArray jattachments = JArray.Parse(attachments1);
                        foreach (JObject jt in jattachments)
                        {
                            sb.Append("insert into ContractFile(projcode,filename,fileurl,assetscode,assetsid) values (");
                            sb.Append("'" + GetString(jb, "projcode") + "','" + GetString(jt, "filename") + "','" + GetString(jt, "fileurl") + "','" + GetString(jo, "assetcode") + "','" + GetString(jo, "assetid") + "');");
                        }
                    }

                    string projattachment = GetString(jo, "projattachments");
                    if (projattachment != "")
                    {
                        JArray projattachments = JArray.Parse(projattachment);
                        foreach (JObject pro in projattachments)
                        {
                            sb.Append("insert into ContractFile(projcode,filename,fileurl,assetscode,assetsid,projfiletype) values (");
                            sb.Append("'" + GetString(jb, "projcode") + "','" + GetString(pro, "filename") + "','" + GetString(pro, "fileurl") + "','" + GetString(jo, "assetcode") + "','" + GetString(jo, "assetid") + "','" + GetString(pro, "projfileurl") + "');");
                        }
                    }
                }
                string attachments = GetString(jb, "attachments");
                if (attachments != "")
                {
                    JArray jattachments = JArray.Parse(attachments);
                    foreach (JObject jt in jattachments)
                    {
                        sb.Append("insert into ContractFile(projcode,filename,fileurl,assetscode,assetsid) values (");
                        sb.Append("'" + GetString(jb, "projcode") + "','" + GetString(jt, "filename") + "','" + GetString(jt, "fileurl") + "','','');");
                    }
                }

                sb.Append(" end");
                await new RepositoryFactory().BaseRepository().ExecuteSql(sb.ToString());
            }
            catch (Exception ex)
            {
                ver = ex.Message;
            }

            return ver;
        }

        public static async Task<string> InsertMessage(string rm, string bm)
        {
            string ver = "";

            try
            {
                string sql = "Insert into Message(Rmessage,Bmessage,rdatetime,bdatetime) values('" + rm + "','" + bm + "','" + DateTime.Now + "','" + DateTime.Now + "')";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
            catch (Exception e)
            {
                ver = e.Message;
            }


            return ver;
        }

        #endregion
        #region "资产导入"
        public static async Task<string> ImportAsset(DataSet ds, string type, int startrow)
        {
            string ver = "";

            try
            {
                string sql = "begin ";
                for (int i = startrow - 1; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow row = ds.Tables[0].Rows[i];
                    switch (type)
                    {
                        case "1":
                            sql += "delete from Asset where zcbm='" + Convert.ToString(row[0]) + "'; insert into Asset values('" + Convert.ToString(row[0]) + "','" + Convert.ToString(row[1]) + "',";
                            sql += "'" + Convert.ToString(row[2]) + "','" + Convert.ToString(row[3]) + "','" + Convert.ToString(row[4]) + "',";
                            sql += "'" + Convert.ToString(row[5]) + "','" + Convert.ToString(row[6]) + "','" + Convert.ToString(row[7]) + "',";
                            string s9 = Convert.ToString(row[9]) == "" ? "0" : Convert.ToString(row[9]);
                            sql += "'" + Convert.ToString(row[8]) + "','" + s9 + "',";
                            string s12 = Convert.ToString(row[12]) == "" ? "0" : Convert.ToString(row[12]);
                            string s10 = Convert.ToString(row[10]) == "" ? "0" : Convert.ToString(row[10]);
                            string s11 = Convert.ToString(row[11]) == "" ? "0" : Convert.ToString(row[11]);
                            sql += "'" + s10 + "','" + s11 + "','" + s12 + "',";
                            string s13 = Convert.ToString(row[13]) == "" ? "0" : Convert.ToString(row[13]);
                            string s14 = Convert.ToString(row[14]) == "" ? "0" : Convert.ToString(row[14]);
                            sql += "'" + s13 + "','" + s14 + "',";
                            string s15 = Convert.ToString(row[15]) == "" ? "0" : Convert.ToString(row[15]);
                            sql += "'" + s15 + "','" + Convert.ToString(row[16]) + "','固定资产',";
                            sql += "'','0','房屋及建筑物','','','','','','','','','','0');";
                            break;
                        case "2":
                            sql += "delete from Asset where zcbm='" + Convert.ToString(row[0]) + "';insert into Asset values('" + Convert.ToString(row[0]) + "','" + Convert.ToString(row[1]) + "',";
                            sql += "'" + Convert.ToString(row[4]) + "','" + Convert.ToString(row[5]) + "','',";
                            sql += "'','0','',";
                            sql += "'" + Convert.ToString(row[7]) + "','0',";
                            s10 = Convert.ToString(row[10]) == "" ? "0" : Convert.ToString(row[10]);
                            string s8 = Convert.ToString(row[8]) == "" ? "0" : Convert.ToString(row[8]);
                            s9 = Convert.ToString(row[9]) == "" ? "0" : Convert.ToString(row[9]);
                            sql += "'" + s8 + "','" + s9 + "','" + s10 + "',";
                            s11 = Convert.ToString(row[11]) == "" ? "0" : Convert.ToString(row[11]);
                            s12 = Convert.ToString(row[12]) == "" ? "0" : Convert.ToString(row[12]);
                            sql += "'" + s11 + "','" + s12 + "',";
                            s13 = Convert.ToString(row[13]) == "" ? "0" : Convert.ToString(row[13]);
                            s14 = Convert.ToString(row[13]) == "" ? "0" : Convert.ToString(row[13]);
                            sql += "'" + s13 + "','" + s14 + "','固定资产',";
                            sql += "'" + Convert.ToString(row[3]) + "','" + Convert.ToString(row[6]) + "','其他类资产','','','','','','','','','','0');";
                            break;
                        case "3":
                            sql += "delete from Asset where zcbm='" + Convert.ToString(row[0]) + "';insert into Asset values('" + Convert.ToString(row[0]) + "','" + Convert.ToString(row[1]) + "',";
                            sql += "'','" + Convert.ToString(row[4]) + "','',";
                            sql += "'','0','',";
                            sql += "'','0',";
                            sql += "'0','0','0',";
                            sql += "'0','0',";
                            sql += "'0','" + Convert.ToString(row[14]) + "','建设用地',";
                            sql += "'','0','" + Convert.ToString(row[2]) + "','" + Convert.ToString(row[3]) + "','" + Convert.ToString(row[5]) + "',";
                            sql += "'" + Convert.ToString(row[6]) + "','" + Convert.ToString(row[7]) + "','" + Convert.ToString(row[8]) + "',";
                            sql += "'" + Convert.ToString(row[10]) + "','" + Convert.ToString(row[11]) + "','" + Convert.ToString(row[12]) + "',";
                            sql += "'" + Convert.ToString(row[13]) + "','" + Convert.ToString(row[9]) + "');";
                            break;
                        case "4":
                            sql += "delete from Asset where zcbm='" + Convert.ToString(row[0]) + "';insert into Asset values('" + Convert.ToString(row[0]) + "','" + Convert.ToString(row[1]) + "',";
                            sql += "'','" + Convert.ToString(row[4]) + "','',";
                            sql += "'','0','',";
                            sql += "'','0',";
                            sql += "'0','0','0',";
                            sql += "'0','0',";
                            sql += "'0','" + Convert.ToString(row[10]) + "','" + Convert.ToString(row[3]) + "',";
                            sql += "'','0','" + Convert.ToString(row[2]) + "','" + Convert.ToString(row[3]) + "','" + Convert.ToString(row[5]) + "',";
                            sql += "'" + Convert.ToString(row[6]) + "','" + Convert.ToString(row[7]) + "','" + Convert.ToString(row[8]) + "',";
                            sql += "'','','',";
                            sql += "'','" + Convert.ToString(row[10]) + "');";
                            break;
                        case "5":
                            sql += "delete from Asset where zcbm='" + Convert.ToString(row[0]) + "';insert into Asset values('" + Convert.ToString(row[0]) + "','" + Convert.ToString(row[1]) + "',";
                            sql += "'','" + Convert.ToString(row[4]) + "','',";
                            sql += "'','0','',";
                            sql += "'','0',";
                            sql += "'0','0','0',";
                            sql += "'0','0',";
                            sql += "'0','" + Convert.ToString(row[10]) + "','" + Convert.ToString(row[3]) + "',";
                            sql += "'','0','" + Convert.ToString(row[2]) + "','" + Convert.ToString(row[3]) + "','" + Convert.ToString(row[5]) + "',";
                            sql += "'" + Convert.ToString(row[6]) + "','" + Convert.ToString(row[7]) + "','" + Convert.ToString(row[8]) + "',";
                            sql += "'','','',";
                            sql += "'','" + Convert.ToString(row[9]) + "');";
                            break;
                        case "6":
                            sql += "delete from Building where zcbm='" + Convert.ToString(row[0]) + "';insert into Building values('" + Convert.ToString(row[0]) + "','" + Convert.ToString(row[1]) + "',";
                            sql += "'" + Convert.ToString(row[2]) + "','" + Convert.ToString(row[3]) + "','" + Convert.ToString(row[4]) + "',";
                            sql += "'" + Convert.ToString(row[5]) + "','" + Convert.ToString(row[6]) + "','" + Convert.ToString(row[7]) + "',";
                            sql += "'" + Convert.ToString(row[8]) + "','" + Convert.ToString(row[9]) + "','" + Convert.ToString(row[10]) + "',";
                            sql += "'" + Convert.ToString(row[11]) + "','" + Convert.ToString(row[12]) + "','" + Convert.ToString(row[13]) + "',";
                            sql += "'" + Convert.ToString(row[14]) + "','" + Convert.ToString(row[15]) + "','" + Convert.ToString(row[16]) + "',";
                            sql += "'" + Convert.ToString(row[17]) + "','" + Convert.ToString(row[18]) + "','" + Convert.ToString(row[19]) + "',";
                            sql += "'" + Convert.ToString(row[20]) + "','" + Convert.ToString(row[21]) + "','" + Convert.ToString(row[22]) + "',";
                            sql += "'" + Convert.ToString(row[23]) + "');";
                            break;
                    }

                }
                sql += " end";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
            }
            catch (Exception ex)
            {
                ver = ex.Message;
            }

            return ver;
        }
        #endregion
        #region “组织接口”
        public static async Task<DataTable> GetOrg(string type, string org)
        {
            string sql = "";
            if (type == "1")
            {
                sql = "select *,org+':'+orgName as oname from Org where len(org)=6 or len(org)=9 and org like '" + org + "%'";
            }
            else
            {
                sql = "select *,org+':'+orgName as oname from Org and org like '" + org + "%'";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetOrg(string type)
        {

            string sql = "";

            sql = @"select a.*,org+' '+orgName as oname,b.text as sj,
c.text as islh
from Org a left join DicInfo b on a.lh=b.value and b.Item='orgsx' left join DicInfo c on a.ss=c.value and c.Item='orgfl' order by org";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<(DataTable, int)> GetOrgByPorg(string org, string orgname, string lh, string ss, string zt, int current, int size)
        {

            string sql = "";
            if (!string.IsNullOrEmpty(org))
            {
                if (org.Contains("320571"))
                {
                    sql = @"select  a.org,a.orgname,a.color,a.porg,a.lh,a.jc,org+' '+orgName as oname,b.text as sj,
c.text as islh, d. text as zt,tjrq
from Org a left join DicInfo b on a.lh=b.value and b.Item='orgsx' left join DicInfo c on a.ss=c.value and c.Item='orgfl' left join DicInfo d on d.Item='orgzt' and a.zt=d.value where 1=1 and (len(org)=15 and org like '320571%') ";
                }
                else
                {
                    sql = @"select  a.org,a.orgname,a.color,a.porg,a.lh,a.jc,org+' '+orgName as oname,b.text as sj,
c.text as islh
from Org a left join DicInfo b on a.lh=b.value and b.Item='orgsx' left join DicInfo c on a.ss=c.value and c.Item='orgfl' where 1=1 ";
                }

                if (!string.IsNullOrEmpty(org))
                {
                    sql += " and org like '%" + org.Trim() + "%'";
                }
                if (orgname != null && orgname != "")
                {
                    sql += " and orgname like '%" + orgname.Trim() + "%'";
                }
                if (!string.IsNullOrEmpty(lh))
                {
                   
                    string str = "";
                    foreach (string s in lh.Split(","))
                    {
                        str += "'"+s+"',";
                    }
                    if (str.Length > 0)
                    {
                        str = str.Substring(0, str.Length - 1);
                        sql += " and ss in(" + str + ")";
                    }
                }
                if (!string.IsNullOrEmpty(ss))
                {

                    string str = "";
                    foreach (string s in ss.Split(","))
                    {
                        str += "'" + s + "',";
                    }
                    if (str.Length > 0)
                    {
                        str = str.Substring(0, str.Length - 1);
                        sql += " and lh in (" + str.Trim() + ")";
                    }
                }
                if (!string.IsNullOrEmpty(zt))
                {

                    string str = "";
                    foreach (string s in zt.Split(","))
                    {
                        str += "'" + s + "',";
                    }
                    if (str.Length > 0)
                    {
                        str = str.Substring(0, str.Length - 1);
                        sql += " and zt in (" + str + ")";
                    }
                }
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "org", true, size, current);
        }

        public static async Task<DataTable> GetOrgTree(string orgname)
        {

            string sql = "";

            sql = @"select org,orgname,color,porg,lh,jc,org+' '+orgName as oname from org where (orgName like '%" + orgname + "%' or org like '%" + orgname + @"%')
union 
select org,orgname,color,porg,lh,jc,org+' '+orgName as oname from org where org in 
(select porg from org where (orgName like '%" + orgname + "%' or org like '%" + orgname + @"%'))
union
select org,orgname,color,porg,lh,jc,org+' '+orgName as oname from Org where org in(
select porg from org where org in 
(select porg from org where (orgName like '%" + orgname + "%' or org like '%" + orgname + @"%')))
union
select org,orgname,color,porg,lh,jc,org+' '+orgName as oname from Org where org in(
select porg from Org where org in(
select porg from org where org in 
(select porg from org where(orgName like '%" + orgname + "%' or org like '%" + orgname + @"%'))))";



            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task SaveOrg(JObject jb)
        {
            try
            {
                string sope = jb["sope"].ToString();
                string sql = "";
                string lh = GetString(jb, "lh");
                if (lh == "量化")
                {
                    lh = "1";
                }
                else if (lh == "非量化")
                {
                    lh = "0";
                }
                else if (lh == "富民")
                {
                    lh = "2";

                }
                else if (lh == "镇属")
                {
                    lh = "3";
                }
                string sx = GetString(jb, "sx");
                if (sx == "")
                {
                    sx = GetString(jb, "ss");
                }
                if (sx.Contains("区"))
                {
                    sx = "0";
                }
                else if (sx.Contains("街道") || sx.Contains("镇"))
                {
                    sx = "1";
                }
                else if (sx.Contains("村") || sx.Contains("社区"))
                {
                    sx = "2";
                }
                else
                {
                    sx = "3";
                }
                string type = GetString(jb, "type");// unit  dept  deptid
                switch (sope)
                {

                    case "add":
                        if (type == "unit")
                        {
                            sql = "delete from org where org='" + GetString(jb, "org") + "';";
                            sql += "insert into org values('" + GetString(jb, "org") + "','" + GetString(jb, "orgname") + "','','" + GetString(jb, "org").Substring(0, GetString(jb, "org").Length - 3) + "','" + lh + "','" + sx + "','" + GetString(jb, "jc") + "','" + GetString(jb, "zt") + "','1','"+DateTime.Today.ToString("yyyy-MM-dd")+"')";
                        }

                        sql += "insert into ht_PermissionCode(bmdm,zzjgdm) values('" + GetString(jb, "deptid") + "','" + GetString(jb, "org") + "')";

                        break;
                    case "edit":
                        if (type == "unit")
                        {
                            sql = "delete from org where org='" + GetString(jb, "org") + "';";
                            sql += "insert into org values('" + GetString(jb, "org") + "','" + GetString(jb, "orgname") + "','','" + GetString(jb, "org").Substring(0, GetString(jb, "org").Length - 3) + "','" + lh + "','" + sx + "','" + GetString(jb, "jc") + "','" + GetString(jb, "zt") + "','1','"+DateTime.Today.ToString("yyyy-MM-dd")+"')";
                        }

                        sql += "delete from ht_PermissionCode where bmdm='" + GetString(jb, "deptid") + "'; insert into ht_PermissionCode(bmdm,zzjgdm) values('" + GetString(jb, "deptid") + "','" + GetString(jb, "org") + "')";

                        break;
                    case "delete":

                        sql = "delete from org  where org='" + GetString(jb, "org") + "'";


                        sql += "delete from ht_PermissionCode  where zzjgdm='" + GetString(jb, "org") + "'";

                        break;
                }
                new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static async Task<DataTable> GetDicInfo(string item)
        {
            string sql = "select * from DicInfo where item='" + item + "' order by value";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region “财务接口”
        public static async Task<DataTable> GetSZMXB(string org, string month, string endmonth, string lh, string ss, string dwzt, string datatype)
        {

            #region "老的语句"
            string sql = "";
            if (org.Contains("320508"))
            {
                string tablename = "SZMXB";
                if (datatype == "1")
                {
                    tablename = "SZMXBDZ";
                }
                sql = @"	declare @org varchar(50);set @org='" + org + "';declare @ny varchar(6);set @ny='" + month + "';declare @eny varchar(6);set @eny='" + endmonth + @"';
            declare @lh varchar(1);set @lh='" + lh + "'; declare @ss varchar(1);set @ss='" + ss + @"';
            declare @tablename varchar(50);
            set @tablename='##temp'+REPLACE(NEWID(),'-','');
           SELECT [AREACODE],[ORGNAME],[ORGCODE],[ISSUE],[INTIME],[STATUS],Type=attribute, Num=value into #t
            FROM " + tablename + @" 
              UNPIVOT
              (
                value FOR attribute IN([INCOME_TOTAL]
                  ,[OPERATING_TOTAL]
                  ,[FARM_PRODUCTS_INCOME]
                  ,[FOREST_SALE_INCOME]
                  ,[GROUP_ASSETS_RENT]
                  ,[MATERIAL_SALE]
                  ,[ELECTRIC_BILL_INCOME]
                  ,[WATER_BILL_INCOME]
                  ,[SERVE_INCOME]
                  ,[LABOUR_SERVE]
                  ,[OTHER_BUSINESS_INCOME]
                  ,[AWARDING_TOTAL]
                  ,[CONTRACT]
                  ,[GROUP_MOBILE_CONTRACT]
                  ,[GROUP_ORCHARD_CONTRACT]
                  ,[GROUP_FISHING_CONTRACT]
                  ,[OTHER_CONTRACT]
                  ,[VILLAGE_INDUSTRY_PROFIT]
                  ,[GRANT_TOTAL]
                  ,[FISCAL_TRANSFERS]
                  ,[CADRES_PAY_GRANT]
                  ,[VILLAGE_OFFICE_BILL]
                  ,[OTHER_GRANT1]
                  ,[OTHER_COUNTRIES_GRANT]
                  ,[ORG_FISCAL_GRANT]
                  ,[WATER_PROJECT_GRANT]
                  ,[SHOW_FLAT_GRANT]
                  ,[ROAD_REPAIR_GRANT]
                  ,[WATER_REPAIR_GRANT]
                  ,[FAMILY_PLANNING_GRANT]
                  ,[OTHER_GRANT2]
                  ,[OTHER_TOTAL]
                  ,[DEPOSIT_INTEREST]
                  ,[LEND_ASSETS_INTEREST]
                  ,[NET_ASSET_INVENTORY]
                  ,[NET_ASSET_CLEAN]
                  ,[BAD_PAYABLE_INCOME]
                  ,[REPARATION_FINE_INCOME]
                  ,[OTHER_INCOME]
                  ,[INVEST_TOTAL]
                  ,[STOCK_SHARES]
                  ,[SECURITIES]
                  ,[SPENDING_TOTAL]
                  ,[BUSINESS_SPENDING]
                  ,[FARM_PRODUCTS_SPENDING]
                  ,[FOREST_SALE_SPENDING]
                  ,[FIXED_ASSETS_REPAIR]
                  ,[ELECTRIC_BILL_SPENDING]
                  ,[WATER_BILL_SPENDING]
                  ,[SERVE_SPENDING]
                  ,[OTHER_BUSINESS_SPENDING]
                  ,[MANAGEMENT_TOTAL]
                  ,[VILLAGE_CADRES_SALARY]
                  ,[OTHER_CADRES_SALARY]
                  ,[GROUP_CADRES_SALARY]
                  ,[CADRES_INSURANCE]
                  ,[CADRES_WEAL]
                  ,[GROUP_WEAL]
                  ,[OFFICE_BILL]
                  ,[WARM_BILL]
                  ,[COMMUNICATION_BILL]
                  ,[TRAFFIC_BILL]
                  ,[TRAVEL_BILL]
                  ,[TRAIN_BILL]
                  ,[SERVE_BILL]
                  ,[MEETING_BILL]
                  ,[OFFICE_WATER_BILL]
                  ,[OFFICE_ELECTRIC_BILL]
                  ,[OTHER_BILL]
                  ,[PAPER_SUBSCRIBE_BILL]
                  ,[MATERIAL_CONSUMPTION]
                  ,[FUEL]
                  ,[MATERIALS]
                  ,[OTHER_CONSUMPTION]
                  ,[FAMILY_PLANNING_SPENDING]
                  ,[ASSETS_DEPRECIATION_MAINTAIN]
                  ,[LOW_VALUE_CONSUMABLES]
                  ,[OTHER_MANAGEMENT_BILL]
                  ,[OTHER_SPENDING]
                  ,[TEMPORARY_SERVANT_SALARY]
                  ,[FARM_STOCK_DEATH]
                  ,[BORROWING_INTEREST]
                  ,[REPARATION_FINE_SPENDING]
                  ,[NET_ASSETS_LOSS]
                  ,[DONATION_SPENDING]
                  ,[ENVIRONMENT_HEALTH_BILL]
                  ,[FLOOD_PREVENTION_SPENDING]
                  ,[SOCIAL_SECURITY_BILL]
                  ,[BAD_PAYABLE_LOSS]
                  ,[PUBLIC_ASSETS_DEPRECIATION]
                  ,[BASE_ASSETS_MAINTAIN]
                  ,[WATER_REPAIR_BILL]
                  ,[ROAD_REPAIR_BILL]
                  ,[PUBLIC_FACILITY_MAINTAIN]
                  ,[OTHER_MAINTAIN])
              ) AS UPV where [ISSUE]=@ny and [ISSUE]<=@eny

 declare @c int;
              set @c=LEN(@org);

              declare @sqls nvarchar(max);
              declare @sql nvarchar(max);
              set @sql='create table '+@tablename+'(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2),zd varchar(50)';
              declare @sqli nvarchar(max);
              declare @sqlii nvarchar(max);
              set @sqli='insert into '+@tablename+' (xh,xm,zd) select xh,xm,UPPER(zd) from TB_TableSet where lx=''1'' and isshow=''1''';
             set @sqls='Update '+@tablename+' set hj=';
               set @sqlii='';
              print @sqli;
            declare @hj varchar(1);
            set @hj='0';
             declare @i int;set @i=0;
               if @c<>9
              begin
              --print '执行的这里'
               if(@lh<>'' and @ss<>'')
              begin
              declare @o varchar(500);
              declare @gx varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o,@gx;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o=REPLACE(@o,'（','');
                 set @o=REPLACE(@o,'）','');
                 SET @sql=@sql+','+@o+' decimal(18,2)';
            	 if(@i<=10)
            	 begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
            	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and isshow=''1'' and ss='''+CONVERT(varchar(1), @ss)+''' and zd=REPLACE(Type,''_'','''')),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
            	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and isshow=''1'' and ss='''+CONVERT(varchar(1), @ss)+'''  and zd=REPLACE(Type,''_'','''')),0)';
            	  end

                     SET @sqls=@sqls+'isnull('+@o+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o,@gx;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh<>'' and @ss='')
              begin

               declare @o1 varchar(500);
              declare @gx1 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o1,@gx1;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o1=REPLACE(@o1,'（','');
                 set @o1=REPLACE(@o1,'）','');
                 SET @sql=@sql+','+@o1+' decimal(18,2)';
            	  if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15   
            	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
            	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
            	  end

                     SET @sqls=@sqls+'isnull('+@o1+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh='' and @ss<>'')
              begin
              print @ss;
                  declare @o2 varchar(500);
              declare @gx2 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org  order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o2,@gx2;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o2=REPLACE(@o2,'（','');
                 set @o2=REPLACE(@o2,'）','');
                 SET @sql=@sql+','+@o2+' decimal(18,2)';

            	 if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
            	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
            	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
            	  end

                     SET @sqls=@sqls+'isnull('+@o2+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
              else
              begin
                    declare @o3 varchar(500);
              declare @gx3 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o3,@gx3;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               --print @i;
               set @o3=REPLACE(@o3,'（','');
                 set @o3=REPLACE(@o3,'）','');
                 SET @sql=@sql+','+@o3+' decimal(18,2)';
            	  if(@i<=20)
            	 begin
            	 print @i;
            	  print @sqli;
            	  set  @sqli+='Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
            	   print @sqli;
            	  end
            	  else
            	  begin
            	  print @i;
            	   print @sqlii;
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
            	   print @sqlii;
            	  end

                     SET @sqls=@sqls+'isnull('+@o3+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
            set @sql=@sql+')';
            print @sql;
            exec(@sql);
            print @sqli;
            print @sqlii;
            exec (@sqli+@sqlii);
            declare @n int;
            set @n=len(@sqls);
            set @sqls=LEFT(@sqls,@n-1);
            print @sqls;
            if(@hj='1')
            begin
            exec(@sqls)
            end
            end
            else
            begin
             if(@lh<>'' and @ss<>'')
              begin
              declare @o4 varchar(500);
              declare @gx4 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and ss=@ss and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o4,@gx4;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o4=REPLACE(@o4,'（','');
                 set @o4=REPLACE(@o4,'）','');
                 SET @sql=@sql+','+@o4+' decimal(18,2)';
            	 if(@i<=10)
            	 begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
            	  where AREACODE='''+@gx4+''' and zd=REPLACE(Type,''_'','''')),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
            	  where AREACODE='''+@gx4+'''  and zd=REPLACE(Type,''_'','''')),0)';
            	  end

                     SET @sqls=@sqls+'isnull('+@o4+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o4,@gx4;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh<>'' and @ss='')
              begin

               declare @o5 varchar(500);
              declare @gx5 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o5,@gx5;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               set @o5=REPLACE(@o5,'（','');
                 set @o5=REPLACE(@o5,'）','');

                 SET @sql=@sql+','+@o5+' decimal(18,2)';
            	  if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
            	  where AREACODE='''+@gx5+'''  and zd=REPLACE(Type,''_'','''')),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
            	  where AREACODE='''+@gx5+''' and zd=REPLACE(Type,''_'','''')),0)';
            	  end

                     SET @sqls=@sqls+'isnull('+@o5+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o5,@gx5;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh='' and @ss<>'')
              begin
              print @ss;
                  declare @o6 varchar(500);
              declare @gx6 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and ss=@ss and isshow='1'  order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o6,@gx6;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o6=REPLACE(@o6,'（','');
                 set @o6=REPLACE(@o6,'）','');
                 SET @sql=@sql+','+@o6+' decimal(18,2)';


            	 if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
            	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
            	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
            	  end

                     SET @sqls=@sqls+'isnull('+@o6+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o6,@gx6;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
              else
              begin
                    declare @o7 varchar(500);
              declare @gx7 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o7,@gx7;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               --print @i;
               print @gx7;
                 set @o7=REPLACE(@o7,'（','');
                 set @o7=REPLACE(@o7,'）','');

                 SET @sql=@sql+','+@o7+' decimal(18,2)';
            	  if(@i<=20)
            	 begin
            	 print @i;
            	  --print @sqli;
            	  set  @sqli+='Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
            	   print @sqli;
            	  end
            	  else
            	  begin
            	  print @i;
            	   print @sqlii;
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
            	   --print @sqlii;
            	  end

                     SET @sqls=@sqls+'isnull('+@o7+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o7,@gx7;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
            set @sql=@sql+')';
            print @sql;
            exec(@sql);
            print @sqli;
            print @sqlii;
            exec (@sqli+@sqlii);
            declare @n1 int;
            set @n1=len(@sqls);
            set @sqls=LEFT(@sqls,@n1-1);
            print @sqls;
            if(@hj='1')
            begin
            exec(@sqls)
            end
            end
            print @i;
            set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
            exec(@sql);
            drop table #t;";
            }
            #endregion
            else
            {

                sql = @"declare @org varchar(50);set @org='" + org + @"';declare @ny varchar(6);set @ny='" + month + @"';declare @eny varchar(6);set @eny='" + endmonth + @"';
declare @lh varchar(1);set @lh='" + lh + @"'; declare @ss varchar(1);set @ss='" + ss + @"';declare @zt varchar(2);set @zt='03';
declare @tablename varchar(50);
set @tablename='##temp'+REPLACE(NEWID(),'-','');
  --select * from #t;
  select distinct * into #tempx from SZMXBX where month>=@ny and month<=@eny;
  declare @c int;
  set @c=LEN(@org);
  declare @col nvarchar(max);
 set @col='';
 declare @i int;
 set @i=0;
  declare @sql nvarchar(max);
  set @sql='select b.xh,b.xm,sl,hj,a.zd';
   if @c=9
  begin
  print '执行的这里'
   if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o varchar(500);
  declare @gx varchar(500);
  declare ColCur cursor for select distinct REPLACE( REPLACE( REPLACE( replace(orgName,'（',''),'）',''),'(',''),')','') as orgname,org From Org where LEFT(org,9)=@org and LEN(org)=15 and lh=@lh and ss=@ss and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o,@gx;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   -- declare @c1 int;
   --set @c1=(select COUNT(1) from szmxb where AREACODE=@gx and ISSUE=@ny)
   --if(@c1>0)
   --begin
   set @sql= @sql+',sum(isnull(['+@gx+'],0))/10000 as '+@o;
   set @col=@col+'['+@o+']+';
   set @i=@i+1;
   --end
		 FETCH NEXT FROM ColCur INTO @o,@gx;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   print '这里ssss'
   declare @o1 varchar(500);
  declare @gx1 varchar(500);
  declare ColCur cursor for select distinct REPLACE( REPLACE( REPLACE( replace(orgName,'（',''),'）',''),'(',''),')','') as orgname,org From Org where  LEFT(org,9)=@org and LEN(org)=15 and lh=@lh and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o1,@gx1;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN 
   -- declare @c111 int;
   --set @c111=(select COUNT(1) from szmxb where AREACODE=@gx1 and ISSUE=@ny)
   --if(@c111>0)
   --begin 
      set @sql= @sql+',sum(isnull(['+@gx1+'],0))/10000 as '+@o1;
   set @col=@col+'['+@o1+']+';
    set @i=@i+1;
    --end
		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
  print '这里ssss'
      declare @o2 varchar(500);
  declare @gx2 varchar(500);
  declare ColCur cursor for select distinct REPLACE( REPLACE( REPLACE( replace(orgName,'（',''),'）',''),'(',''),')','') as orgname,org From Org where  LEFT(org,9)=@org and LEN(org)=15 and ss=@ss and zt<>'03' and isshow='1'  order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o2,@gx2;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   -- declare @c2 int;
   --set @c2=(select COUNT(1) from szmxb where AREACODE=@gx2 and ISSUE=@ny)
   --if(@c2>0)
   --begin 
     set @sql= @sql+',sum(isnull(['+@gx2+'],0))/10000 as '+@o2;
   set @col=@col+'['+@o2+']+';
    set @i=@i+1;
    --end
		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
  print '这里ssss'
        declare @o3 varchar(500);
  declare @gx3 varchar(500);
  declare ColCur cursor for select distinct REPLACE( REPLACE( REPLACE( replace(orgName,'（',''),'）',''),'(',''),')','') as orgname,org From Org where  LEFT(org,9)=@org and LEN(org)=15 and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o3,@gx3;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   -- declare @c3 int;
   --set @c3=(select COUNT(1) from szmxb where AREACODE=@gx3 and ISSUE=@ny)
   --if(@c3>0)
   --begin 
     set @sql= @sql+',sum(isnull(['+@gx3+'],0))/10000 as '+@o3;
   set @col=@col+'['+@o3+']+';
    set @i=@i+1;
   -- end
		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else
  begin
  print '这里1'
   declare @o31 varchar(500);
  declare @gx31 varchar(500);
  declare ColCur cursor for select distinct REPLACE( REPLACE( REPLACE( replace(orgName,'（',''),'）',''),'(',''),')','') as orgname,org From Org where  LEFT(org,9)=@org and LEN(org)=15 and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o31,@gx31;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   -- declare @c31 int;
   --set @c31=(select COUNT(1) from szmxb where AREACODE=@gx31 and ISSUE=@ny)
   --if(@c111>0)
   --begin 
     set @sql= @sql+',sum(isnull(['+@gx31+'],0))/10000 as '+@o31;
   set @col=@col+'['+@o31+']+';
    set @i=@i+1;
    --end
		 FETCH NEXT FROM ColCur INTO @o31,@gx31;  
   END  
close ColCur;  
deallocate ColCur;  
  end
set @sql=@sql+' into '+@tablename+' from #tempx a inner join TB_TableSet b on a.xh=b.xh and lx=''1'' and isshow=''1'' where (month>='''+@ny+''' and month<='''+@eny+''') group by b.xh,b.xm,sl,hj,a.zd';
print @sql;
exec(@sql);
drop table #tempx;
if(@col<>'')
begin
set @col=left(@col,len(@col)-1);
set @sql='update  '+@tablename+' set hj=('+@col+')';
print @sql;
exec(@sql);
print @i;
set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int,xh);drop table '+@tablename+';';
exec(@sql);
end
  end
  else
  begin
  print 'ssss'
  set @sql='select b.xh,b.xm,sl,hj,a.zd,';
  declare ColCur cursor for select distinct REPLACE( REPLACE( REPLACE( replace(orgName,'（',''),'）',''),'(',''),')','') as orgname,org From Org where LEFT(org,@c)=@org and LEN(org)=@c+3 and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o,@gx;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   print @zt
   print dbo.Get_Cols(@gx,@lh,@ss,@zt,@ny);
   if dbo.Get_Cols(@gx,@lh,@ss,@zt,@ny)<>''
   begin
   set @sql=@sql+'sum('+dbo.Get_Cols(@gx,@lh,@ss,@zt,@ny)+')/10000 as '+@o+',';
   end
   else
   begin
   set @sql=@sql+'0 as '+@o+',';
   end
   set @col=@col+'['+@o+']+';
set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o,@gx;  
   END  
close ColCur;  
deallocate ColCur;  
set @sql=LEFT(@sql,LEN(@sql)-1);
set @sql=@sql+' into '+@tablename+' from #tempx a inner join TB_TableSet b on a.xh=b.xh and lx=''1'' and isshow=''1'' where (month>='''+@ny+''' and month<='''+@eny+''') group by b.xh,b.xm,sl,hj,a.zd';
print @sql;
exec(@sql);
set @col=left(@col,len(@col)-1);
set @sql='update  '+@tablename+' set hj=('+@col+')';
print @sql;
exec(@sql);
set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int,xh);drop table '+@tablename+';';
print @sql;
exec(@sql);
drop table #tempx;
  end
";
            }
            NLog.Logger log = LogManager.GetCurrentClassLogger();
            log.Log(LogLevel.Info, sql);
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetKMYEB(string org, string month, string endmonth, string lh, string ss, string dwzt, string datatype)
        {
            string tablename = "KMYEB";
            if (datatype == "1")
            {
                tablename = "KMYEBDZ";
            }
            string sql = @"
declare @org varchar(50);set @org='" + org + "';declare @ny varchar(6);set @ny='" + month + "';declare @eny varchar(6);set @eny='" + endmonth + @"';
declare @lh varchar(1);set @lh='" + lh + "'; declare @ss varchar(1);set @ss='" + ss + @"';declare @tablename varchar(50);declare @zt varchar(2);set @zt='03';
set @tablename='##temp'+REPLACE(NEWID(),'-','');
SELECT [AREACODE],[ORGNAME],[ORGCODE],[ISSUE],[INTIME],[STATUS],Type=attribute, Num=value into #t
FROM " + tablename + @"
  UNPIVOT
  (
    value FOR attribute IN([BORROWING_ASSETS_TOTAL],[CASH],[BANK_DEPOSIT],[OTHER_MONETARY_FUNDS],[NOTES_RECEIVABLE],[SHORT_TERM_INVESTMENT]
           ,[ACCOUNTS_RECEIVABLE],[TRANSACTIONS_DEBIT],[INVENTORY_ITEMS],[LIVESTOCK_ASSETS],[FOREST_ASSET],[LONG_ASSETS],[LONG_ASSETS_TOTAL]
           ,[DEPRECIATION],[ASSETS_CLEAN],[PRO_CONSTRUCTION],[OTHER_ASSETS],[OTHER_ASSETS_AMORTIZATION],[HOLD_SALE_ASSETS],[LONG_TERM_AMORTIZATION]
           ,[PRODUCTION_LABOR_COST],[DISBURSEMENT_UNIT_COST],[BORROWING_PROFIT_TOTAL],[BUSINESS_SPENDING],[TAXES_APPEND_SPENDING],[MANAGEMENT_TOTAL]
           ,[PUBLIC_SPENDING],[FARM_DEVELOP_SPENDING],[OTHER_SPENDING],[CREDIT_LIABILITIES_TOTAL],[SHORT_TERM_BORROWING],[ACCOUNTS_PAYABLE],[PAYABLE_TAXES],[PAYABLE_SALARY]
           ,[PAYABLE_WELFARE],[LONG_BORROWINGS],[DISCUSSION],[SPECIAL_PAYMENT],[TRANSACTION_CREDIT],[CREDIT_OWNER_TOTAL],[CAPITAL],[PROVIDENT_FUNDS],[YEAR_EARNINGS]
           ,[INCOME_DISTRIBUTION],[CREDIT_PROFIT_TOTAL],[OPERATING_INCOME],[AWARDING_INCOME],[FARM_TAXED_RETURN],[OTHER_INCOME],[INVESTMENT_INCOME])
  ) AS UPV where [ISSUE]>=@ny and [ISSUE]<=@eny

  --select * from #t;
  declare @c int;
  set @c=LEN(@org);

  declare @sqls nvarchar(max);
  declare @sql nvarchar(max);
  set @sql='create table '+@tablename+'(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2),zd varchar(50)';
  declare @sqli nvarchar(max);
  declare @sqlii nvarchar(max);
  set @sqli='insert into '+@tablename+' (xh,xm,zd) select xh,xm,UPPER(zd) from TB_TableSet where lx=''3'' and isshow=''1''';
  set @sqls='Update '+@tablename+' set hj=';
   set @sqlii='';
  print @sqli;
declare @hj varchar(1);
set @hj='0';
 declare @i int;set @i=0;
   if @c<>9
  begin
  --print '执行的这里'
   if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o varchar(500);
  declare @gx varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o,@gx;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o=REPLACE(@o,'（','');
     set @o=REPLACE(@o,'）','');
     SET @sql=@sql+','+@o+' decimal(18,2)';
	 if(@i<=10)
	 begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o,@gx;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   
   declare @o1 varchar(500);
  declare @gx1 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o1,@gx1;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o1=REPLACE(@o1,'（','');
     set @o1=REPLACE(@o1,'）','');
     SET @sql=@sql+','+@o1+' decimal(18,2)';
	  if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15   
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o1+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
      declare @o2 varchar(500);
  declare @gx2 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org  order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o2,@gx2;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o2=REPLACE(@o2,'（','');
     set @o2=REPLACE(@o2,'）','');
     SET @sql=@sql+','+@o2+' decimal(18,2)';
	 
	 if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o2+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
        declare @o3 varchar(500);
  declare @gx3 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o3,@gx3;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   set @o3=REPLACE(@o3,'（','');
     set @o3=REPLACE(@o3,'）','');
     SET @sql=@sql+','+@o3+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o3+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else
  begin
          declare @o31 varchar(500);
  declare @gx31 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o31,@gx31;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   set @o31=REPLACE(@o31,'（','');
     set @o31=REPLACE(@o31,'）','');
     SET @sql=@sql+','+@o31+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o31+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o31+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o31+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o31,@gx31;  
   END  
close ColCur;  
deallocate ColCur;
  end

set @sql=@sql+')';
print @sql;
exec(@sql);
print @sqli;
print @sqlii;
exec (@sqli+@sqlii);
declare @n int;
set @n=len(@sqls);
set @sqls=LEFT(@sqls,@n-1);
print @sqls;
if(@hj='1')
begin
exec(@sqls)
end
end
else
begin
 if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o4 varchar(500);
  declare @gx4 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and ss=@ss and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o4,@gx4;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o4=REPLACE(@o4,'（','');
     set @o4=REPLACE(@o4,'）','');
     SET @sql=@sql+','+@o4+' decimal(18,2)';
	 if(@i<=10)
	 begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx4+''' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx4+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o4+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o4,@gx4;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   
   declare @o5 varchar(500);
  declare @gx5 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o5,@gx5;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   set @o5=REPLACE(@o5,'（','');
     set @o5=REPLACE(@o5,'）','');

     SET @sql=@sql+','+@o5+' decimal(18,2)';
	  if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx5+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx5+''' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o5+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o5,@gx5;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
      declare @o6 varchar(500);
  declare @gx6 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and ss=@ss and isshow='1'  order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o6,@gx6;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o6=REPLACE(@o6,'（','');
     set @o6=REPLACE(@o6,'）','');
     SET @sql=@sql+','+@o6+' decimal(18,2)';
	

	 if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o6+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o6,@gx6;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
        declare @o7 varchar(500);
  declare @gx7 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15  and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o7,@gx7;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   print @gx7;
     set @o7=REPLACE(@o7,'（','');
     set @o7=REPLACE(@o7,'）','');

     SET @sql=@sql+','+@o7+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  --print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
	   --print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o7+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o7,@gx7;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else
  begin
        declare @o71 varchar(500);
  declare @gx71 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o71,@gx71;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   print @gx71;
     set @o71=REPLACE(@o71,'（','');
     set @o71=REPLACE(@o71,'）','');

     SET @sql=@sql+','+@o71+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  --print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o71+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx71+''' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o71+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx71+''' and zd=REPLACE(Type,''_'','''')),0)';
	   --print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o71+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o71,@gx71;  
   END  
close ColCur;  
deallocate ColCur;  
  end
set @sql=@sql+')';
print @sql;
exec(@sql);
print @sqli;
print @sqlii;
exec (@sqli+@sqlii);
declare @n1 int;
set @n1=len(@sqls);
set @sqls=LEFT(@sqls,@n1-1);
print @sqls;
if(@hj='1')
begin
exec(@sqls)
end
end
print @i;
set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
exec(@sql);
drop table #t;
";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZWZQB(string org, string month, string endmonth, string lh, string ss, string dwzt, string datatype)
        {
            string tablename = "ZWZQB";
            if (datatype == "1")
            {
                tablename = "ZWZQBDZ";
            }
            string sql = @"
declare @org varchar(50);set @org='" + org + "';declare @ny varchar(6);set @ny='" + month + "';declare @eny varchar(6);set @eny='" + endmonth + @"';
declare @lh varchar(1);set @lh='" + lh + "'; declare @ss varchar(1);set @ss='" + ss + @"';declare @tablename varchar(50);declare @zt varchar(2);set @zt='03';
set @tablename='##temp'+REPLACE(NEWID(),'-','');
SELECT [AREACODE],[ORGNAME],[ORGCODE],[ISSUE],[INTIME],[STATUS],Type=attribute, Num=value into #t
FROM " + tablename + @"
  UNPIVOT
  (
    value FOR attribute IN([DEBT_BALANCE_TOTAL]
      ,[ACCOUNTS_RECEIVABLE]
      ,[INDIVIDUAL_OWE_GROUP]
      ,[DEBT_CURRENT_BALANCE]
      ,[CURRENT_ACCOUNTS_RECEIVABLE]
      ,[PAYABLE_SALARY]
      ,[PAYABLE_WELFARE]
      ,[SHORT_TERM_BORROWING]
      ,[LONG_TERM_BORROWING]
      ,[GROUP_OWE_INDIVIDUAL])
  ) AS UPV where [ISSUE]>=@ny and [ISSUE]<=@eny
--select * from #t;
  declare @c int;
  set @c=LEN(@org);

  declare @sqls nvarchar(max);
  declare @sql nvarchar(max);
  set @sql='create table '+@tablename+'(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2),zd varchar(50)';
  declare @sqli nvarchar(max);
  declare @sqlii nvarchar(max);
  set @sqli='insert into '+@tablename+' (xh,xm,zd) select xh,xm,UPPER(zd) from TB_TableSet where lx=''2''';
  set @sqls='Update '+@tablename+' set hj=';
   set @sqlii='';
  print @sqli;
declare @hj varchar(1);
set @hj='0';
 declare @i int;set @i=0;
  if @c<>9
  begin
  --print '执行的这里'
   if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o varchar(500);
  declare @gx varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o,@gx;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o=REPLACE(@o,'（','');
     set @o=REPLACE(@o,'）','');
     SET @sql=@sql+','+@o+' decimal(18,2)';
	 if(@i<=10)
	 begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o,@gx;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   
   declare @o1 varchar(500);
  declare @gx1 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o1,@gx1;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o1=REPLACE(@o1,'（','');
     set @o1=REPLACE(@o1,'）','');
     SET @sql=@sql+','+@o1+' decimal(18,2)';
	  if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15   
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o1+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
      declare @o2 varchar(500);
  declare @gx2 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org  order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o2,@gx2;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o2=REPLACE(@o2,'（','');
     set @o2=REPLACE(@o2,'）','');
     SET @sql=@sql+','+@o2+' decimal(18,2)';
	 
	 if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o2+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
        declare @o3 varchar(500);
  declare @gx3 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o3,@gx3;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   set @o3=REPLACE(@o3,'（','');
     set @o3=REPLACE(@o3,'）','');
     SET @sql=@sql+','+@o3+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o3+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else
  begin
        declare @o31 varchar(500);
  declare @gx31 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o31,@gx31;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   set @o31=REPLACE(@o31,'（','');
     set @o31=REPLACE(@o31,'）','');
     SET @sql=@sql+','+@o31+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o31+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o31+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o31+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o31,@gx31;  
   END  
close ColCur;  
deallocate ColCur;  
  end
set @sql=@sql+')';
print @sql;
exec(@sql);
print @sqli;
print @sqlii;
exec (@sqli+@sqlii);
declare @n int;
set @n=len(@sqls);
set @sqls=LEFT(@sqls,@n-1);
print @sqls;
if(@hj='1')
begin
exec(@sqls)
end
end
else
begin
 if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o4 varchar(500);
  declare @gx4 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and ss=@ss and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o4,@gx4;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o4=REPLACE(@o4,'（','');
     set @o4=REPLACE(@o4,'）','');
     SET @sql=@sql+','+@o4+' decimal(18,2)';
	 if(@i<=10)
	 begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx4+''' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx4+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o4+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o4,@gx4;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   
   declare @o5 varchar(500);
  declare @gx5 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o5,@gx5;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   set @o5=REPLACE(@o5,'（','');
     set @o5=REPLACE(@o5,'）','');

     SET @sql=@sql+','+@o5+' decimal(18,2)';
	  if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx5+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx5+''' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o5+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o5,@gx5;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
      declare @o6 varchar(500);
  declare @gx6 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and ss=@ss and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o6,@gx6;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o6=REPLACE(@o6,'（','');
     set @o6=REPLACE(@o6,'）','');
     SET @sql=@sql+','+@o6+' decimal(18,2)';
	

	 if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o6+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o6,@gx6;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
        declare @o7 varchar(500);
  declare @gx7 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o7,@gx7;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   print @gx7;
     set @o7=REPLACE(@o7,'（','');
     set @o7=REPLACE(@o7,'）','');

     SET @sql=@sql+','+@o7+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  --print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
	   --print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o7+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o7,@gx7;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else
  begin
        declare @o71 varchar(500);
  declare @gx71 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and isshow='1'  order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o71,@gx71;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   --print @gx7;
     set @o71=REPLACE(@o71,'（','');
     set @o71=REPLACE(@o71,'）','');

     SET @sql=@sql+','+@o71+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  --print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o71+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx71+''' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o71+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx71+''' and zd=REPLACE(Type,''_'','''')),0)';
	   --print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o71+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o71,@gx71;  
   END  
close ColCur;  
deallocate ColCur;  
  end
set @sql=@sql+')';
print @sql;
exec(@sql);
print @sqli;
print @sqlii;
exec (@sqli+@sqlii);
declare @n1 int;
set @n1=len(@sqls);
set @sqls=LEFT(@sqls,@n1-1);
print @sqls;
if(@hj='1')
begin
exec(@sqls)
end
end
print @i;
set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
exec(@sql);
drop table #t;
";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZCFZB(string org, string month, string endmonth, string lh, string ss, string dwzt, string datatype)
        {
            string tablename = "ZCFZB";
            if (datatype == "1")
            {
                tablename = "ZCFZBDZ";
            }
            string sql = @"
declare @org varchar(50);set @org='" + org + "';declare @ny varchar(6);set @ny='" + month + "';declare @eny varchar(6);set @eny='" + endmonth + @"';
declare @lh varchar(1);set @lh='" + lh + "'; declare @ss varchar(1);set @ss='" + ss + @"';declare @tablename varchar(50);declare @zt varchar(2);set @zt='03';
set @tablename='##temp'+REPLACE(NEWID(),'-','');
SELECT [AREACODE],[ORGNAME],[ORGCODE],[ISSUE],[INTIME],[STATUS],Type=attribute, Num=value into #t
FROM " + tablename + @"
  UNPIVOT
  (
    value FOR attribute IN([ASSETS_TOTAL]
      ,[CURRENT_ASSETS]
      ,[MONETARY_TOTAL]
      ,[BANK_DEPOSIT]
      ,[SHORT_TERM_INVESTMENTS]
      ,[RECEIVABLE_TOTAL]
      ,[ACCOUNTS_RECEIVABLE]
      ,[TRANSACTIONS_DEBIT]
      ,[INVENTORY_TOTAL]
      ,[INVENTORY_ITEMS]
      ,[AGRICULTURAL_TOTAL]
      ,[LIVESTOCK_ASSETS]
      ,[FOREST_ASSET]
      ,[LONG_ASSETS]
      ,[LONG_ASSETS_TOTAL]
      ,[ORIGINAL_COST]
      ,[DEPRECIATION]
      ,[NET_WORTH]
      ,[ASSETS_CLEAN]
      ,[PRO_CONSTRUCTION]
      ,[INTANGIBLE_ASSETS]
      ,[OTHER_ASSETS]
      ,[LIABILITIES_TOTAL]
      ,[FLOW_LIABILITIES_TOTAL]
      ,[SHORT_TERM_BORROWING]
      ,[PAYABLE_TOTAL]
      ,[TRANSACTION_CREDIT]
      ,[ACCOUNTS_PAYABLE]
      ,[SPECIAL_PAYMENT]
      ,[PAYABLE_SALARY]
      ,[PAYABLE_WELFARE]
      ,[LONG_LIABILITIES_TOTAL]
      ,[LONG_BORROWINGS]
      ,[DISCUSSION]
      ,[OWNER_TOTAL]
      ,[COLLECTIVE_CAPITAL]
      ,[COMMUNITY_CHEST]
      ,[WELFARE_FUND]
      ,[UNDISTRIBUTED]
      ,[LIABILITIES_OWNER_SUM],fzhj,syzqy,zb,gjgyj,wfpsy,syzquhj)
  ) AS UPV where [ISSUE]>=@ny and [ISSUE]<=@eny
--select * from #t;
  declare @c int;
  set @c=LEN(@org);

  declare @sqls nvarchar(max);
  declare @sql nvarchar(max);
  set @sql='create table '+@tablename+'(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2),zd varchar(50)';
  declare @sqli nvarchar(max);
  declare @sqlii nvarchar(max);
  set @sqli='insert into '+@tablename+' (xh,xm,zd) select xh,xm,UPPER(zd) from TB_TableSet where lx=''4''  and isshow=''1''';
  set @sqls='Update '+@tablename+' set hj=';
   set @sqlii='';
  print @sqli;
declare @hj varchar(1);
set @hj='0';
 declare @i int;set @i=0;
   if @c<>9
  begin
  --print '执行的这里'
   if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o varchar(500);
  declare @gx varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o,@gx;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o=REPLACE(@o,'（','');
     set @o=REPLACE(@o,'）','');
     SET @sql=@sql+','+@o+' decimal(18,2)';
	 if(@i<=10)
	 begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o,@gx;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   
   declare @o1 varchar(500);
  declare @gx1 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o1,@gx1;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o1=REPLACE(@o1,'（','');
     set @o1=REPLACE(@o1,'）','');
     SET @sql=@sql+','+@o1+' decimal(18,2)';
	  if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15   
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o1+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
      declare @o2 varchar(500);
  declare @gx2 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org  order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o2,@gx2;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o2=REPLACE(@o2,'（','');
     set @o2=REPLACE(@o2,'）','');
     SET @sql=@sql+','+@o2+' decimal(18,2)';
	 
	 if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o2+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
        declare @o3 varchar(500);
  declare @gx3 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o3,@gx3;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   set @o3=REPLACE(@o3,'（','');
     set @o3=REPLACE(@o3,'）','');
     SET @sql=@sql+','+@o3+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o3+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
   END  
close ColCur;  
deallocate ColCur;  
  end
   else
  begin
        declare @o31 varchar(500);
  declare @gx31 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o31,@gx31;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   set @o3=REPLACE(@o31,'（','');
     set @o3=REPLACE(@o31,'）','');
     SET @sql=@sql+','+@o31+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o31+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o31+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o31+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o31,@gx31;  
   END  
close ColCur;  
deallocate ColCur;  
  end
set @sql=@sql+')';
print @sql;
exec(@sql);
print @sqli;
print @sqlii;
exec (@sqli+@sqlii);
declare @n int;
set @n=len(@sqls);
set @sqls=LEFT(@sqls,@n-1);
print @sqls;
if(@hj='1')
begin
exec(@sqls)
end
end
else
begin
 if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o4 varchar(500);
  declare @gx4 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and ss=@ss and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o4,@gx4;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o4=REPLACE(@o4,'（','');
     set @o4=REPLACE(@o4,'）','');
     SET @sql=@sql+','+@o4+' decimal(18,2)';
	 if(@i<=10)
	 begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx4+''' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx4+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o4+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o4,@gx4;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   
   declare @o5 varchar(500);
  declare @gx5 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o5,@gx5;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   set @o5=REPLACE(@o5,'（','');
     set @o5=REPLACE(@o5,'）','');

     SET @sql=@sql+','+@o5+' decimal(18,2)';
	  if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx5+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx5+''' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o5+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o5,@gx5;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
      declare @o6 varchar(500);
  declare @gx6 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and ss=@ss and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o6,@gx6;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o6=REPLACE(@o6,'（','');
     set @o6=REPLACE(@o6,'）','');
     SET @sql=@sql+','+@o6+' decimal(18,2)';
	

	 if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o6+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o6,@gx6;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
        declare @o7 varchar(500);
  declare @gx7 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o7,@gx7;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   print @gx7;
     set @o7=REPLACE(@o7,'（','');
     set @o7=REPLACE(@o7,'）','');

     SET @sql=@sql+','+@o7+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  --print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
	   --print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o7+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o7,@gx7;  
   END  
close ColCur;  
deallocate ColCur;  
  end
   else
  begin
        declare @o71 varchar(500);
  declare @gx71 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o71,@gx71;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   --print @gx7;
     set @o71=REPLACE(@o71,'（','');
     set @o71=REPLACE(@o71,'）','');

     SET @sql=@sql+','+@o71+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  --print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o71+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx71+''' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o71+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx71+''' and zd=REPLACE(Type,''_'','''')),0)';
	   --print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o71+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o71,@gx71;  
   END  
close ColCur;  
deallocate ColCur;  
  end
set @sql=@sql+')';
print @sql;
exec(@sql);
print @sqli;
print @sqlii;
exec (@sqli+@sqlii);
declare @n1 int;
set @n1=len(@sqls);
set @sqls=LEFT(@sqls,@n1-1);
print @sqls;
if(@hj='1')
begin
exec(@sqls)
end
end
print @i;
set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
exec(@sql);
drop table #t;
";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetSYFPB(string org, string month, string endmonth, string lh, string ss, string dwzt, string datatype)
        {
            string tablename = "SYFPB";
            if (datatype == "1")
            {
                tablename = "SYFPBDZ";
            }
            string sql = @"
declare @org varchar(50);set @org='" + org + "';declare @ny varchar(6);set @ny='" + month + "';declare @eny varchar(6);set @eny='" + endmonth + @"';
declare @lh varchar(1);set @lh='" + lh + "'; declare @ss varchar(1);set @ss='" + ss + @"';declare @tablename varchar(50);declare @zt varchar(2);set @zt='03';
set @tablename='##temp'+REPLACE(NEWID(),'-','');
SELECT [AREACODE],[ORGNAME],[ORGCODE],[ISSUE],[INTIME],[STATUS],Type=attribute, Num=value into #t
FROM " + tablename + @"
  UNPIVOT
  (
    value FOR attribute IN([INCOME_TOTAL],[PROFITS_TOTAL],[OPERATING_INCOME],[AWARDING_TOTAL],[TENANT],[PROFITS_HANDED],[INCOME_INVESTMENT],[SUBSIDIES_TOTAL]
      ,[TRANSFER_PAYMENTS],[OTHER_INCOME],[SPENDING_TOTAL],[BUSINESS_SPENDING],[MANAGEMENT_TOTAL],[CADRES_REMUNERATION],[PUBLICITY_FEES],[OFFICE_SPENDING],[OTHER_SPENDING]
      ,[EARNED_INCOME],[YEAR_EARNINGS],[BEGIN_UNDISTRIBUTED],[OTHER_TRANSFERRED],[DISTRIBUTABLE_TOTAL],[COMMUNITY_CHEST],[PAYABLE_WELFARE],[OUTSIDE_FINANCING] ,[FARMERS_DISTRIBUTION]
      ,[OTHER_EARNINGS],[END_UNDISTRIBUTED],[BUD_AMT],[SP_BUD_AMT],[MG_BUD_AMT],[SALARY_BUD_AMT],[PRESS_BUD_AMT],[OFFICE_BUD_AMT],[OTHER_BUD_AMT])
  ) AS UPV where [ISSUE]>=@ny and [ISSUE]<=@eny

  --select * from #t;
  declare @c int;
  set @c=LEN(@org);

  declare @sqls nvarchar(max);
  declare @sql nvarchar(max);
  set @sql='create table '+@tablename+'(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2),zd varchar(50)';
  declare @sqli nvarchar(max);
  declare @sqlii nvarchar(max);
  set @sqli='insert into '+@tablename+' (xh,xm,zd) select xh,xm,UPPER(zd) from TB_TableSet where lx=''5''  and isshow=''1''';
  set @sqls='Update '+@tablename+' set hj=';
   set @sqlii='';
  print @sqli;
declare @hj varchar(1);
set @hj='0';
 declare @i int;set @i=0;
   if @c<>9
  begin
  --print '执行的这里'
   if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o varchar(500);
  declare @gx varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o,@gx;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o=REPLACE(@o,'（','');
     set @o=REPLACE(@o,'）','');
     SET @sql=@sql+','+@o+' decimal(18,2)';
	 if(@i<=10)
	 begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o,@gx;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   
   declare @o1 varchar(500);
  declare @gx1 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o1,@gx1;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o1=REPLACE(@o1,'（','');
     set @o1=REPLACE(@o1,'）','');
     SET @sql=@sql+','+@o1+' decimal(18,2)';
	  if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15   
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o1+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and lh='''+CONVERT(varchar(1), @lh)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o1+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
      declare @o2 varchar(500);
  declare @gx2 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org  order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o2,@gx2;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o2=REPLACE(@o2,'（','');
     set @o2=REPLACE(@o2,'）','');
     SET @sql=@sql+','+@o2+' decimal(18,2)';
	 
	 if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o2+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o2+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
        declare @o3 varchar(500);
  declare @gx3 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o3,@gx3;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   set @o3=REPLACE(@o3,'（','');
     set @o3=REPLACE(@o3,'）','');
     SET @sql=@sql+','+@o3+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o3+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o3+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else
  begin
        declare @o31 varchar(500);
  declare @gx31 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o31,@gx31;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   set @o31=REPLACE(@o31,'（','');
     set @o31=REPLACE(@o31,'）','');
     SET @sql=@sql+','+@o31+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o31+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o31+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where left(AREACODE,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1'' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o31+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o31,@gx31;  
   END  
close ColCur;  
deallocate ColCur;  
  end
set @sql=@sql+')';
print @sql;
exec(@sql);
print @sqli;
print @sqlii;
exec (@sqli+@sqlii);
declare @n int;
set @n=len(@sqls);
set @sqls=LEFT(@sqls,@n-1);
print @sqls;
if(@hj='1')
begin
exec(@sqls)
end
end
else
begin
 if(@lh<>'' and @ss<>'' and @zt<>'')
  begin
  declare @o4 varchar(500);
  declare @gx4 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and ss=@ss and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o4,@gx4;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o4=REPLACE(@o4,'（','');
     set @o4=REPLACE(@o4,'）','');
     SET @sql=@sql+','+@o4+' decimal(18,2)';
	 if(@i<=10)
	 begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx4+''' and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o4+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx4+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o4+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o4,@gx4;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh<>'' and @ss='' and @zt<>'')
  begin
   
   declare @o5 varchar(500);
  declare @gx5 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o5,@gx5;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   set @o5=REPLACE(@o5,'（','');
     set @o5=REPLACE(@o5,'）','');

     SET @sql=@sql+','+@o5+' decimal(18,2)';
	  if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx5+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o5+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx5+''' and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o5+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o5,@gx5;  
   END  
close ColCur;  
deallocate ColCur;  

  end
  else if(@lh='' and @ss<>'' and @zt<>'')
  begin
  print @ss;
      declare @o6 varchar(500);
  declare @gx6 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and ss=@ss and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o6,@gx6;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
    set @o6=REPLACE(@o6,'（','');
     set @o6=REPLACE(@o6,'）','');
     SET @sql=@sql+','+@o6+' decimal(18,2)';
	

	 if(@i<=10)
	begin
	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15    
	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end
	  else
	  begin
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o6+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15  
	  where AREACODE='''+@gx6+'''  and zd=REPLACE(Type,''_'','''')),0)';
	  end

         SET @sqls=@sqls+'+isnull('+@o6+',0)'+'+';
set @hj='1';
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o6,@gx6;  
   END  
close ColCur;  
deallocate ColCur;  
  end
  else if(@lh='' and @ss='' and @zt<>'')
  begin
        declare @o71 varchar(500);
  declare @gx71 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and zt<>'03' and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o71,@gx71;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   print @gx71;
     set @o71=REPLACE(@o71,'（','');
     set @o71=REPLACE(@o71,'）','');

     SET @sql=@sql+','+@o71+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  --print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o71+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx71+''' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o71+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx71+''' and zd=REPLACE(Type,''_'','''')),0)';
	   --print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o71+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o71,@gx71;  
   END  
close ColCur;  
deallocate ColCur;  
  end
   else
  begin
        declare @o7 varchar(500);
  declare @gx7 varchar(500);
  declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and isshow='1' order by org;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o7,@gx7;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
   --print @i;
   print @gx7;
     set @o7=REPLACE(@o7,'（','');
     set @o7=REPLACE(@o7,'）','');

     SET @sql=@sql+','+@o7+' decimal(18,2)';
	  if(@i<=20)
	 begin
	 print @i;
	  --print @sqli;
	  set  @sqli+='Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
	   print @sqli;
	  end
	  else
	  begin
	  print @i;
	   print @sqlii;
	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o7+'=isnull((select sum(Num)/10000 from #t a left join org b on a.AREACODE=b.org and len(b.org)=15 where AREACODE='''+@gx7+''' and zd=REPLACE(Type,''_'','''')),0)';
	   --print @sqlii;
	  end

         SET @sqls=@sqls+'+isnull('+@o7+',0)'+'+';
set @hj='1';
		 print @sqli;
		 print @sqlii;
		 set @i=@i+1;
		 FETCH NEXT FROM ColCur INTO @o7,@gx7;  
   END  
close ColCur;  
deallocate ColCur;  
  end
set @sql=@sql+')';
print @sql;
exec(@sql);
print @sqli;
print @sqlii;
exec (@sqli+@sqlii);
declare @n1 int;
set @n1=len(@sqls);
set @sqls=LEFT(@sqls,@n1-1);
print @sqls;
if(@hj='1')
begin
exec(@sqls)
end
end
print @i;
set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
exec(@sql);
drop table #t;
";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZCFZBCW(string org, string month, string endmonth, string lh, string ss, string dwzt, string datatype)
        {
            #region 
            //string tablename = "SYFPB";
            //if (datatype == "1")
            //{
            //    tablename = "SYFPBDZ";
            //}
            string sql = @"
            declare @org varchar(50);set @org='" + org + "';declare @ny varchar(6);set @ny='" + month + @"';
            declare @lh varchar(1);set @lh='" + lh + "'; declare @ss varchar(1);set @ss='" + ss + @"';declare @tablename varchar(50);declare @zt varchar(2);set @zt='03';
set @tablename='##temp'+REPLACE(NEWID(),'-','');
declare @c int;
              set @c=LEN(@org);
              print @c;
              declare @sqls nvarchar(max);
              declare @sql nvarchar(max);
              set @sql='create table '+@tablename+'(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2)';
              declare @sqli nvarchar(max);
              declare @sqlii nvarchar(max);
              set @sqli='insert into '+@tablename+' (xh,xm) select xh,xm from TB_TableSet where lx=''6''';
              --exec(@sqli);
              set @sqls='Update '+@tablename+' set hj=';
               set @sqlii='';
              print @sqli;
            declare @hj varchar(1);
            set @hj='0';
             declare @i int;set @i=0;
               if @c<>9
              begin
              print '执行的这里'
               if(@lh<>'' and @ss<>'' and @zt<>'')
              begin
              declare @o varchar(500);
              declare @gx varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o,@gx;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o=REPLACE(@o,'（','');
                 set @o=REPLACE(@o,'）','');
                 SET @sql=@sql+','+@o+' decimal(18,2)';
            	 if(@i<=10)
            	 begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org    
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx+'''  and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and kjqj='+@ny+'   and a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org   
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx+'''  and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and kjqj='+@ny+'   and a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o,@gx;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh<>'' and @ss='' and @zt<>'')
              begin

               declare @o1 varchar(500);
              declare @gx1 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o1,@gx1;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o1=REPLACE(@o1,'（','');
                 set @o1=REPLACE(@o1,'）','');
                 SET @sql=@sql+','+@o1+' decimal(18,2)';
            	  if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o1+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org   
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+'''  and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o1+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+'''  and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o1+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh='' and @ss<>'' and @zt<>'')
              begin
              print @ss;
                  declare @o2 varchar(500);
              declare @gx2 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o2,@gx2;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o2=REPLACE(@o2,'（','');
                 set @o2=REPLACE(@o2,'）','');
                 SET @sql=@sql+','+@o2+' decimal(18,2)';
            	if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o2+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and len(b.org)=15 and ss='''+CONVERT(varchar(1), @ss)+''' and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o2+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and len(b.org)=15 and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and kjqj='+@ny+' and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o2+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
              else if(@lh='' and @ss='' and @zt<>'')
              begin
                    declare @o3 varchar(500);
              declare @gx3 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o3,@gx3;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               print @i;
               set @o3=REPLACE(@o3,'（','');
                 set @o3=REPLACE(@o3,'）','');
                 SET @sql=@sql+','+@o3+' decimal(18,2)';

            	 if(@i<=20)
            	begin
            	print '这里';
            	 print @sqli;
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o3+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+'''  and len(b.org)=15  and zt<>'''+CONVERT(varchar(2),@zt)+''' and kjqj='+@ny+'  and isshow=''1'' and  a.xh='+@tablename+'.xh),0)';
            	  print @sqli;
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o3+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org 
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+'''  and len(b.org)=15  and zt<>'''+CONVERT(varchar(2),@zt)+''' and kjqj='+@ny+'  and isshow=''1'' and  a.xh='+@tablename+'.xh),0)';
            	  print @sqlii;
            	  end

                     SET @sqls=@sqls+'+isnull('+@o3+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
			   else
              begin
                    declare @o31 varchar(500);
              declare @gx31 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o31,@gx31;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               print @i;
               set @o31=REPLACE(@o31,'（','');
                 set @o31=REPLACE(@o31,'）','');
                 SET @sql=@sql+','+@o31+' decimal(18,2)';

            	 if(@i<=20)
            	begin
            	print '这里';
            	 print @sqli;
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o31+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1''  and len(b.org)=15  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  print @sqli;
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o31+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org 
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and isshow=''1''  and len(b.org)=15  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  print @sqlii;
            	  end

                     SET @sqls=@sqls+'+isnull('+@o31+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o31,@gx31;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
            set @sql=@sql+')';
            print @sql;
            exec(@sql);
            print @sqli;
            print @sqlii;
            exec (@sqli+@sqlii);
            declare @n int;
            set @n=len(@sqls);
            set @sqls=LEFT(@sqls,@n-1);
            print @sqls;
            if(@hj='1')
            begin
            exec(@sqls)
            end
            end
            else
            begin
             if(@lh<>'' and @ss<>'' and @zt<>'')
              begin
              declare @o4 varchar(500);
              declare @gx4 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and ss=@ss and zt<>'03' and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o4,@gx4;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o4=REPLACE(@o4,'（','');
                 set @o4=REPLACE(@o4,'）','');
                 SET @sql=@sql+','+@o4+' decimal(18,2)';
            	 if(@i<=10)
            	 begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o4+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org    
            	  where a.org='''+@gx4+'''  and len(b.org)=15  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o4+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org 
            	  where a.org='''+@gx4+'''  and len(b.org)=15   and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o4+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o4,@gx4;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh<>'' and @ss='' and @zt<>'')
              begin

               declare @o5 varchar(500);
              declare @gx5 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and zt<>'03' and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o5,@gx5;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               set @o5=REPLACE(@o5,'（','');
                 set @o5=REPLACE(@o5,'）','');

                 SET @sql=@sql+','+@o5+' decimal(18,2)';
            	  if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o5+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org    
            	  where a.org='''+@gx5+'''  and len(b.org)=15   and kjqj='+@ny+' and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o5+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            	  where a.org='''+@gx5+'''  and len(b.org)=15  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o5+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o5,@gx5;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh='' and @ss<>'' and @zt<>'')
              begin
              print @ss;
                  declare @o6 varchar(500);
              declare @gx6 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and ss=@ss and zt<>'03' and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o6,@gx6;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o6=REPLACE(@o6,'（','');
                 set @o6=REPLACE(@o6,'）','');
                 SET @sql=@sql+','+@o6+' decimal(18,2)';


            	 if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o6+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            	  where a.org='''+@gx6+'''   and len(b.org)=15  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o6+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org 
            	  where a.org='''+@gx6+'''  and len(b.org)=15   and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o6+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o6,@gx6;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
              else if(@lh='' and @ss='' and @zt<>'')
              begin
                    declare @o7 varchar(500);
              declare @gx7 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and zt<>'03' and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o7,@gx7;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               --print @i;
               print @gx7;
                 set @o7=REPLACE(@o7,'（','');
                 set @o7=REPLACE(@o7,'）','');

                 SET @sql=@sql+','+@o7+' decimal(18,2)';
            	  if(@i<=20)
            	 begin
            	 print @i;
            	  --print @sqli;
            	  set  @sqli+='Update '+@tablename+' set '+@o7+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  where a.org='''+@gx7+'''  and len(b.org)=15  and kjqj='+@ny+' and  a.xh='+@tablename+'.xh),0)';
            	   print @sqli;
            	  end
            	  else
            	  begin
            	  print @i;
            	   print @sqlii;
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o7+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  where a.org='''+@gx7+'''  and len(b.org)=15  and kjqj='+@ny+' and  a.xh='+@tablename+'.xh),0)';
            	   --print @sqlii;
            	  end

                     SET @sqls=@sqls+'+isnull('+@o7+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o7,@gx7;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
              else
			  begin
                    declare @o71 varchar(500);
              declare @gx71 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15  and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o71,@gx71;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               --print @i;
               --print @gx7;
                 set @o71=REPLACE(@o71,'（','');
                 set @o71=REPLACE(@o71,'）','');

                 SET @sql=@sql+','+@o71+' decimal(18,2)';
            	  if(@i<=20)
            	 begin
            	 print @i;
            	  --print @sqli;
            	  set  @sqli+='Update '+@tablename+' set '+@o71+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  where a.org='''+@gx71+'''  and len(b.org)=15  and kjqj='+@ny+' and  a.xh='+@tablename+'.xh),0)';
            	   print @sqli;
            	  end
            	  else
            	  begin
            	  print @i;
            	   print @sqlii;
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o71+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  where a.org='''+@gx71+'''  and len(b.org)=15  and kjqj='+@ny+' and  a.xh='+@tablename+'.xh),0)';
            	   --print @sqlii;
            	  end

                     SET @sqls=@sqls+'+isnull('+@o71+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o71,@gx71;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
            set @sql=@sql+')';
            print @sql;
            exec(@sql);
            print @sqli;
            print @sqlii;
            exec (@sqli+@sqlii);
            declare @n1 int;
            set @n1=len(@sqls);
            set @sqls=LEFT(@sqls,@n1-1);
            print @sqls;
            if(@hj='1')
            begin
            exec(@sqls)
            end
            end
            print @i;
            set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
            exec(@sql);
            ";
            #endregion

            #region 
            //            string sql = @"declare @org varchar(50);set @org='"+org+"';declare @ny varchar(6);set @ny='"+month+@"';
            //declare @lh varchar(1); set @lh = '"+lh+"'; declare @ss varchar(1); set @ss = '"+ss+ @"'; declare @tablename varchar(50);
            //            set @tablename = '##temp' + REPLACE(NEWID(), '-', '');
            //            declare @c int;
            //  set @c=LEN(@org);
            //  print @c;
            //  declare @sqls nvarchar(max);
            //  declare @sql nvarchar(max);
            //  set @sql='create table '+@tablename+'(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2)';
            //  declare @sqli nvarchar(max);
            //  declare @sqlii nvarchar(max);
            //  set @sqli='insert into '+@tablename+' (xh,xm) select xh,xm from TB_TableSet where lx=''6''';
            //  --exec(@sqli);
            //  set @sqls='Update '+@tablename+' set hj=';
            //   set @sqlii='';
            //  print @sqli;
            //declare @hj varchar(1);
            //set @hj='0';
            // declare @i int;set @i=0;


            //  print '执行的这里'
            //   if(@lh<>'' and @ss<>'')
            //  begin
            //  declare @o varchar(500);
            //  declare @gx varchar(500);
            //  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            //open ColCur;  
            //FETCH NEXT FROM ColCur INTO @o,@gx;  
            //WHILE (@@FETCH_STATUS = 0)  
            //   BEGIN  
            //    set @o=REPLACE(@o,'（','');
            //     set @o=REPLACE(@o,'）','');
            //     SET @sql=@sql+','+@o+' decimal(18,2)';
            //	 if(@i<=10)
            //	 begin
            //	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org    
            //	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx+'''  and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and kjqj='+@ny+'   and a.xh='+@tablename+'.xh),0)';
            //	  end
            //	  else
            //	  begin
            //	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org   
            //	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx+'''  and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+'''  and kjqj='+@ny+'   and a.xh='+@tablename+'.xh),0)';
            //	  end

            //         SET @sqls=@sqls+'+isnull('+@o+',0)'+'+';
            //set @hj='1';
            //		 set @i=@i+1;
            //		 FETCH NEXT FROM ColCur INTO @o,@gx;  
            //   END  
            //close ColCur;  
            //deallocate ColCur;  

            //  end
            //  else if(@lh<>'' and @ss='')
            //  begin

            //   declare @o1 varchar(500);
            //  declare @gx1 varchar(500);
            //  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            //open ColCur;  
            //FETCH NEXT FROM ColCur INTO @o1,@gx1;  
            //WHILE (@@FETCH_STATUS = 0)  
            //   BEGIN  
            //    set @o1=REPLACE(@o1,'（','');
            //     set @o1=REPLACE(@o1,'）','');
            //     SET @sql=@sql+','+@o1+' decimal(18,2)';
            //	  if(@i<=10)
            //	begin
            //	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o1+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org   
            //	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+'''  and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+'''    and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            //	  end
            //	  else
            //	  begin
            //	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o1+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            //	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+'''  and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+'''   and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            //	  end

            //         SET @sqls=@sqls+'+isnull('+@o1+',0)'+'+';
            //set @hj='1';
            //		 set @i=@i+1;
            //		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
            //   END  
            //close ColCur;  
            //deallocate ColCur;  

            //  end
            //  else if(@lh='' and @ss<>'')
            //  begin
            //  print @ss;
            //      declare @o2 varchar(500);
            //  declare @gx2 varchar(500);
            //  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            //open ColCur;  
            //FETCH NEXT FROM ColCur INTO @o2,@gx2;  
            //WHILE (@@FETCH_STATUS = 0)  
            //   BEGIN  
            //    set @o2=REPLACE(@o2,'（','');
            //     set @o2=REPLACE(@o2,'）','');
            //     SET @sql=@sql+','+@o2+' decimal(18,2)';
            //	if(@i<=10)
            //	begin
            //	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o2+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            //	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and len(b.org)=15 and ss='''+CONVERT(varchar(1), @ss)+'''   and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            //	  end
            //	  else
            //	  begin
            //	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o2+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            //	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+'''  and len(b.org)=15 and ss='''+CONVERT(varchar(1), @ss)+'''   and kjqj='+@ny+' and  a.xh='+@tablename+'.xh),0)';
            //	  end

            //         SET @sqls=@sqls+'+isnull('+@o2+',0)'+'+';
            //set @hj='1';
            //		 set @i=@i+1;
            //		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
            //   END  
            //close ColCur;  
            //deallocate ColCur;  
            //  end
            //  else
            //  begin
            //        declare @o3 varchar(500);
            //  declare @gx3 varchar(500);
            //  declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            //open ColCur;  
            //FETCH NEXT FROM ColCur INTO @o3,@gx3;  
            //WHILE (@@FETCH_STATUS = 0)  
            //   BEGIN  
            //   print @i;
            //   set @o3=REPLACE(@o3,'（','');
            //     set @o3=REPLACE(@o3,'）','');
            //     SET @sql=@sql+','+@o3+' decimal(18,2)';

            //	 if(@i<=20)
            //	begin
            //	print '这里';
            //	 print @sqli;
            //	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o3+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org  
            //	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+'''  and len(b.org)=15  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            //	  print @sqli;
            //	  end
            //	  else
            //	  begin
            //	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o3+'=isnull((select sum(qm)/10000 from ZCFZBCW a left join org b on a.org=b.org 
            //	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+'''  and len(b.org)=15  and kjqj='+@ny+'  and  a.xh='+@tablename+'.xh),0)';
            //	  print @sqlii;
            //	  end

            //         SET @sqls=@sqls+'+isnull('+@o3+',0)'+'+';
            //set @hj='1';
            //		 print @sqli;
            //		 print @sqlii;
            //		 set @i=@i+1;
            //		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
            //   END  
            //close ColCur;  
            //deallocate ColCur;  
            //  end
            //set @sql=@sql+')';
            //print @sql;
            //exec(@sql);
            //print @sqli;
            //print @sqlii;
            //exec (@sqli+@sqlii);
            //declare @n int;
            //set @n=len(@sqls);
            //set @sqls=LEFT(@sqls,@n-1);
            //print @sqls;
            //if(@hj='1')
            //begin
            //exec(@sqls)
            //end


            //print @i;
            //set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+';drop table '+@tablename+';';
            //exec(@sql);

            //            ";
            #endregion
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetLRBCW(string org, string month, string endmonth, string lh, string ss, string dwzt)
        {
            #region
            string sql = @"
            declare @org varchar(50);set @org='" + org + "';declare @ny varchar(6);set @ny='" + month + @"';declare @eny varchar(6);set @eny='" + endmonth + @"';
            declare @lh varchar(1);set @lh='" + lh + "'; declare @ss varchar(1);set @ss='" + ss + @"';declare @tablename varchar(50);declare @zt varchar(2);set @zt='03';
set @tablename='##temp'+REPLACE(NEWID(),'-','');
declare @c int;
              set @c=LEN(@org);
              print @c;
              declare @sqls nvarchar(max);
              declare @sql nvarchar(max);
              set @sql='create table '+@tablename+'(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2)';
              declare @sqli nvarchar(max);
              declare @sqlii nvarchar(max);
              set @sqli='insert into '+@tablename+' (xh,xm) select xh,xm from TB_TableSet where lx=''7''';
              --exec(@sqli);
              set @sqls='Update '+@tablename+' set hj=';
               set @sqlii='';
              print @sqli;
            declare @hj varchar(1);
            set @hj='0';
             declare @i int;set @i=0;
               if @c<>9
              begin
              print '执行的这里'
               if(@lh<>'' and @ss<>'' and @zt<>'')
              begin
              declare @o varchar(500);
              declare @gx varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o,@gx;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o=REPLACE(@o,'（','');
                 set @o=REPLACE(@o,'）','');
                 SET @sql=@sql+','+@o+' decimal(18,2)';
            	 if(@i<=10)
            	 begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org     
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and isshow=''1'' and zt<>'''+CONVERT(varchar(2),@zt)+''' and kjqj>='+@ny+' and kjqj<='+@eny+'   and a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org 
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx+''' and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+''' and ss='''+CONVERT(varchar(1), @ss)+''' and isshow=''1''  and zt<>'''+CONVERT(varchar(2),@zt)+'''  and kjqj>='+@ny+' and kjqj<='+@eny+'  and a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o,@gx;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh<>'' and @ss='' and @zt<>'')
              begin

               declare @o1 varchar(500);
              declare @gx1 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o1,@gx1;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o1=REPLACE(@o1,'（','');
                 set @o1=REPLACE(@o1,'）','');
                 SET @sql=@sql+','+@o1+' decimal(18,2)';
            	  if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o1+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org   
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+'''   and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o1+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx1+''' and len(b.org)=15 and lh='''+CONVERT(varchar(1), @lh)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o1+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh='' and @ss<>'' and @zt<>'')
              begin
              print @ss;
                  declare @o2 varchar(500);
              declare @gx2 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org  order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o2,@gx2;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o2=REPLACE(@o2,'（','');
                 set @o2=REPLACE(@o2,'）','');
                 SET @sql=@sql+','+@o2+' decimal(18,2)';
            	if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o2+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+''' and len(b.org)=15 and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o2+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org 
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx2+''' and len(b.org)=15 and ss='''+CONVERT(varchar(1), @ss)+'''  and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1''  and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o2+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o2,@gx2;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
              else if(@lh='' and @ss='' and @zt<>'')
              begin
                    declare @o3 varchar(500);
              declare @gx3 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o3,@gx3;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               print @i;
               set @o3=REPLACE(@o3,'（','');
                 set @o3=REPLACE(@o3,'）','');
                 SET @sql=@sql+','+@o3+' decimal(18,2)';

            	 if(@i<=20)
            	begin
            	print '这里';
            	 print @sqli;
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o3+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org    
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+''' and len(b.org)=15 and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and kjqj>='+@ny+'  and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  print @sqli;
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o3+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org 
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx3+''' and len(b.org)=15 and zt<>'''+CONVERT(varchar(2),@zt)+''' and isshow=''1'' and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  print @sqlii;
            	  end

                     SET @sqls=@sqls+'+isnull('+@o3+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o3,@gx3;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
			   else
              begin
                    declare @o31 varchar(500);
              declare @gx31 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where porg=@org order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o31,@gx31;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               print @i;
               set @o31=REPLACE(@o31,'（','');
                 set @o31=REPLACE(@o31,'）','');
                 SET @sql=@sql+','+@o31+' decimal(18,2)';

            	 if(@i<=20)
            	begin
            	print '这里';
            	 print @sqli;
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o31+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org    
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and len(b.org)=15 and isshow=''1''  and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  print @sqli;
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o31+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org 
            	  where left(a.org,'+CONVERT(varchar(5), @c+3)+')='''+@gx31+''' and len(b.org)=15 and isshow=''1'' and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  print @sqlii;
            	  end

                     SET @sqls=@sqls+'+isnull('+@o3+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o31,@gx31;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
            set @sql=@sql+')';
            print @sql;
            exec(@sql);
            print @sqli;
            print @sqlii;
            exec (@sqli+@sqlii);
            declare @n int;
            set @n=len(@sqls);
            set @sqls=LEFT(@sqls,@n-1);
            print @sqls;
            if(@hj='1')
            begin
            exec(@sqls)
            end
            end
            else
            begin
             if(@lh<>'' and @ss<>'' and @zt<>'')
              begin
              declare @o4 varchar(500);
              declare @gx4 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and ss=@ss and zt<>'03' and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o4,@gx4;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o4=REPLACE(@o4,'（','');
                 set @o4=REPLACE(@o4,'）','');
                 SET @sql=@sql+','+@o4+' decimal(18,2)';
            	 if(@i<=10)
            	 begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o4+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org     
            	  where a.org='''+@gx4+'''  and len(b.org)=15 and kjqj>='+@ny+' and kjqj<='+@eny+'  and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o4+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  
            	  where a.org='''+@gx4+''' and len(b.org)=15   and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o4+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o4,@gx4;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh<>'' and @ss='' and @zt<>'')
              begin

               declare @o5 varchar(500);
              declare @gx5 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and lh=@lh and zt<>'03' and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o5,@gx5;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               set @o5=REPLACE(@o5,'（','');
                 set @o5=REPLACE(@o5,'）','');

                 SET @sql=@sql+','+@o5+' decimal(18,2)';
            	  if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o5+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org    
            	  where a.org='''+@gx5+''' and len(b.org)=15  and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o5+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  
            	  where a.org='''+@gx5+''' and len(b.org)=15  and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o5+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o5,@gx5;  
               END  
            close ColCur;  
            deallocate ColCur;  

              end
              else if(@lh='' and @ss<>'' and @zt<>'')
              begin
              print @ss;
                  declare @o6 varchar(500);
              declare @gx6 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and ss=@ss and zt<>'03' and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o6,@gx6;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
                set @o6=REPLACE(@o6,'（','');
                 set @o6=REPLACE(@o6,'）','');
                 SET @sql=@sql+','+@o6+' decimal(18,2)';


            	 if(@i<=10)
            	begin
            	  set  @sqli= @sqli+'Update '+@tablename+' set '+@o6+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org     
            	  where a.org='''+@gx6+''' and len(b.org)=15  and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end
            	  else
            	  begin
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o6+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  
            	  where a.org='''+@gx6+''' and len(b.org)=15  and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	  end

                     SET @sqls=@sqls+'+isnull('+@o6+',0)'+'+';
            set @hj='1';
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o6,@gx6;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
              else if(@lh='' and @ss='' and @zt<>'')
              begin
                    declare @o7 varchar(500);
              declare @gx7 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and zt<>'03' and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o7,@gx7;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               --print @i;
               print @gx7;
                 set @o7=REPLACE(@o7,'（','');
                 set @o7=REPLACE(@o7,'）','');

                 SET @sql=@sql+','+@o7+' decimal(18,2)';
            	  if(@i<=20)
            	 begin
            	 print @i;
            	  --print @sqli;
            	  set  @sqli+='Update '+@tablename+' set '+@o7+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org where a.org='''+@gx7+''' and len(b.org)=15 and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	   print @sqli;
            	  end
            	  else
            	  begin
            	  print @i;
            	   print @sqlii;
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o7+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  where a.org='''+@gx7+''' and len(b.org)=15 and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	   --print @sqlii;
            	  end

                     SET @sqls=@sqls+'+isnull('+@o7+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o7,@gx7;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end

			   else
              begin
                    declare @o71 varchar(500);
              declare @gx71 varchar(500);
              declare ColCur cursor for select distinct orgName,org From Org where left(porg,9)=@org and len(org)=15 and isshow='1' order by org;  
            open ColCur;  
            FETCH NEXT FROM ColCur INTO @o71,@gx71;  
            WHILE (@@FETCH_STATUS = 0)  
               BEGIN  
               --print @i;
               --print @gx7;
                 set @o71=REPLACE(@o71,'（','');
                 set @o71=REPLACE(@o71,'）','');

                 SET @sql=@sql+','+@o71+' decimal(18,2)';
            	  if(@i<=20)
            	 begin
            	 print @i;
            	  --print @sqli;
            	  set  @sqli+='Update '+@tablename+' set '+@o71+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org where a.org='''+@gx71+''' and len(b.org)=15 and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	   print @sqli;
            	  end
            	  else
            	  begin
            	  print @i;
            	   print @sqlii;
            	  set  @sqlii= @sqlii+'Update '+@tablename+' set '+@o71+'=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  where a.org='''+@gx71+''' and len(b.org)=15 and kjqj>='+@ny+' and kjqj<='+@eny+' and  a.xh='+@tablename+'.xh),0)';
            	   --print @sqlii;
            	  end

                     SET @sqls=@sqls+'+isnull('+@o7+',0)'+'+';
            set @hj='1';
            		 print @sqli;
            		 print @sqlii;
            		 set @i=@i+1;
            		 FETCH NEXT FROM ColCur INTO @o71,@gx71;  
               END  
            close ColCur;  
            deallocate ColCur;  
              end
            set @sql=@sql+')';
            print @sql;
            exec(@sql);
            print @sqli;
            print @sqlii;
            exec (@sqli+@sqlii);
            declare @n1 int;
            set @n1=len(@sqls);
            set @sqls=LEFT(@sqls,@n1-1);
            print @sqls;
            if(@hj='1')
            begin
            exec(@sqls)
            end
            end
            print @i;
            set @sql='update '+@tablename+' set sl='+CONVERT(varchar(5),@i)+'; select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
            exec(@sql);
            ";
            #endregion
            #region 
            //            string sql = @"declare @org varchar(50);set @org='"+org+"';declare @ny varchar(6);set @ny='" + month + @"';
            //declare @lh varchar(1); set @lh = '" + lh + "'; declare @ss varchar(1); set @ss = '" + ss + @"'; declare @tablename varchar(50);
            //            set @tablename = '##temp' + REPLACE(NEWID(), '-', '');
            //            --select * from #t;
            //  declare @c int;
            //            set @c = LEN(@org);
            //            print @c;
            //            declare @sqls nvarchar(max);
            //            declare @sql nvarchar(max);
            //            set @sql = 'create table ' + @tablename + '(xh varchar(50),xm varchar(50),sl int,hj decimal(18,2)';
            //            declare @sqli nvarchar(max);
            //            declare @sqlii nvarchar(max);
            //            set @sqli = 'insert into ' + @tablename + ' (xh,xm) select xh,xm from TB_TableSet where lx=''7''';
            //            --exec(@sqli);
            //            set @sqls = 'Update ' + @tablename + ' set hj=';
            //            set @sqlii = '';
            //            print @sqli;
            //            declare @hj varchar(1);
            //            set @hj = '0';
            //            declare @i int; set @i = 0;


            //            print '执行的这里'
            //   if (@lh <> '' and @ss<>'')
            //  begin
            //  declare @o varchar(500);
            //            declare @gx varchar(500);
            //            declare ColCur cursor for select distinct orgName, org From Org where porg = @org order by org;
            //            open ColCur;
            //            FETCH NEXT FROM ColCur INTO @o, @gx;
            //            WHILE(@@FETCH_STATUS = 0)
            //   BEGIN
            //    set @o = REPLACE(@o, '（', '');
            //            set @o = REPLACE(@o, '）', '');
            //            SET @sql = @sql + ',' + @o + ' decimal(18,2)';
            //            if (@i <= 10)
            //                begin
            //                 set  @sqli = @sqli + 'Update ' + @tablename + ' set ' + @o + '=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org     

            //      where left(a.org,'+CONVERT(varchar(5), @c+3)+')= '''+@gx+''' and len(b.org)= 15 and lh = '''+CONVERT(varchar(1), @lh)+''' and ss = '''+CONVERT(varchar(1), @ss)+''' and kjqj = '+@ny+'   and a.xh = '+@tablename+'.xh),0)';

            //      end

            //      else
            //                begin
            //                set  @sqlii = @sqlii + 'Update ' + @tablename + ' set ' + @o + '=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org 

            //      where left(a.org,'+CONVERT(varchar(5), @c+3)+')= '''+@gx+''' and len(b.org)= 15 and lh = '''+CONVERT(varchar(1), @lh)+''' and ss = '''+CONVERT(varchar(1), @ss)+'''  and kjqj = '+@ny+'   and a.xh = '+@tablename+'.xh),0)';

            //      end

            //         SET @sqls = @sqls + '+isnull(' + @o + ',0)' + '+';
            //            set @hj = '1';
            //            set @i = @i + 1;
            //            FETCH NEXT FROM ColCur INTO @o, @gx;
            //            END
            //         close ColCur;
            //            deallocate ColCur;

            //            end
            //  else if (@lh <> '' and @ss = '')
            //  begin

            //   declare @o1 varchar(500);
            //            declare @gx1 varchar(500);
            //            declare ColCur cursor for select distinct orgName, org From Org where porg = @org order by org;
            //            open ColCur;
            //            FETCH NEXT FROM ColCur INTO @o1, @gx1;
            //            WHILE(@@FETCH_STATUS = 0)
            //   BEGIN
            //    set @o1 = REPLACE(@o1, '（', '');
            //            set @o1 = REPLACE(@o1, '）', '');
            //            SET @sql = @sql + ',' + @o1 + ' decimal(18,2)';
            //            if (@i <= 10)
            //                begin
            //                  set  @sqli = @sqli + 'Update ' + @tablename + ' set ' + @o1 + '=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org   

            //      where left(a.org,'+CONVERT(varchar(5), @c+3)+')= '''+@gx1+''' and len(b.org)= 15 and lh = '''+CONVERT(varchar(1), @lh)+'''    and kjqj = '+@ny+' and a.xh = '+@tablename+'.xh),0)';

            //      end

            //      else
            //                begin
            //                set  @sqlii = @sqlii + 'Update ' + @tablename + ' set ' + @o1 + '=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  

            //      where left(a.org,'+CONVERT(varchar(5), @c+3)+')= '''+@gx1+''' and len(b.org)= 15 and lh = '''+CONVERT(varchar(1), @lh)+'''   and kjqj = '+@ny+' and a.xh = '+@tablename+'.xh),0)';

            //      end

            //         SET @sqls = @sqls + '+isnull(' + @o1 + ',0)' + '+';
            //            set @hj = '1';
            //            set @i = @i + 1;
            //            FETCH NEXT FROM ColCur INTO @o1, @gx1;
            //            END
            //         close ColCur;
            //            deallocate ColCur;

            //            end
            //  else if (@lh = '' and @ss<>'')
            //  begin
            //  print @ss;
            //            declare @o2 varchar(500);
            //            declare @gx2 varchar(500);
            //            declare ColCur cursor for select distinct orgName, org From Org where porg = @org  order by org;
            //            open ColCur;
            //            FETCH NEXT FROM ColCur INTO @o2, @gx2;
            //            WHILE(@@FETCH_STATUS = 0)
            //   BEGIN
            //    set @o2 = REPLACE(@o2, '（', '');
            //            set @o2 = REPLACE(@o2, '）', '');
            //            SET @sql = @sql + ',' + @o2 + ' decimal(18,2)';
            //            if (@i <= 10)
            //                begin
            //                  set  @sqli = @sqli + 'Update ' + @tablename + ' set ' + @o2 + '=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org  

            //      where left(a.org,'+CONVERT(varchar(5), @c+3)+')= '''+@gx2+''' and len(b.org)= 15 and ss = '''+CONVERT(varchar(1), @ss)+'''   and kjqj = '+@ny+' and a.xh = '+@tablename+'.xh),0)';

            //      end

            //      else
            //                begin
            //                set  @sqlii = @sqlii + 'Update ' + @tablename + ' set ' + @o2 + '=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org 

            //      where left(a.org,'+CONVERT(varchar(5), @c+3)+')= '''+@gx2+''' and len(b.org)= 15 and ss = '''+CONVERT(varchar(1), @ss)+'''   and kjqj = '+@ny+' and a.xh = '+@tablename+'.xh),0)';

            //      end

            //         SET @sqls = @sqls + '+isnull(' + @o2 + ',0)' + '+';
            //            set @hj = '1';
            //            set @i = @i + 1;
            //            FETCH NEXT FROM ColCur INTO @o2, @gx2;
            //            END
            //         close ColCur;
            //            deallocate ColCur;
            //            end
            //  else
            //                begin
            //                      declare @o3 varchar(500);
            //            declare @gx3 varchar(500);
            //            declare ColCur cursor for select distinct orgName, org From Org where porg = @org order by org;
            //            open ColCur;
            //            FETCH NEXT FROM ColCur INTO @o3, @gx3;
            //            WHILE(@@FETCH_STATUS = 0)
            //   BEGIN
            //   print @i;
            //            set @o3 = REPLACE(@o3, '（', '');
            //            set @o3 = REPLACE(@o3, '）', '');
            //            SET @sql = @sql + ',' + @o3 + ' decimal(18,2)';

            //            if (@i <= 20)
            //                begin
            //                print '这里';
            //            print @sqli;
            //            set @sqli = @sqli + 'Update ' + @tablename + ' set ' + @o3 + '=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org    

            //      where left(a.org,'+CONVERT(varchar(5), @c+3)+')= '''+@gx3+''' and len(b.org)= 15  and kjqj = '+@ny+' and a.xh = '+@tablename+'.xh),0)';

            //      print @sqli;
            //            end

            //      else
            //                begin
            //                set  @sqlii = @sqlii + 'Update ' + @tablename + ' set ' + @o3 + '=isnull((select sum(bnlj)/10000 from LRBCW a left join org b on a.org=b.org 

            //      where left(a.org,'+CONVERT(varchar(5), @c+3)+')= '''+@gx3+''' and len(b.org)= 15  and kjqj = '+@ny+' and a.xh = '+@tablename+'.xh),0)';

            //      print @sqlii;
            //            end

            //               SET @sqls = @sqls + '+isnull(' + @o3 + ',0)' + '+';
            //            set @hj = '1';
            //            print @sqli;
            //            print @sqlii;
            //            set @i = @i + 1;
            //            FETCH NEXT FROM ColCur INTO @o3, @gx3;
            //            END
            //         close ColCur;
            //            deallocate ColCur;
            //            end
            //          set @sql = @sql + ')';
            //            print @sql;
            //            exec(@sql);
            //            print @sqli;
            //            print @sqlii;
            //            exec(@sqli + @sqlii);
            //            declare @n int;
            //            set @n = len(@sqls);
            //            set @sqls = LEFT(@sqls, @n - 1);
            //            print @sqls;
            //            if (@hj = '1')
            //                begin
            //exec(@sqls)
            //end


            //print @i;
            //            set @sql = 'update ' + @tablename + ' set sl=' + CONVERT(varchar(5), @i) + '; select * from ' + @tablename + ';drop table ' + @tablename + ';';
            //            exec(@sql);
            //            ";
            #endregion
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }


        #endregion
        #region  "支付凭证"

        public static async Task<string> ImportZF(JArray ja)
        {
            string ver = "";
            try
            {
                if (ja.Count > 0)
                {
                    string sql = "begin ";
                    foreach (JObject jb in ja)
                    {
                        string je = "0";
                        if (GetString(jb, "je") != "")
                        {
                            je = GetString(jb, "je");
                        }
                        sql += @"delete from ZFPZ where djbh='" + GetString(jb, "djbh") + @"'INSERT INTO ZFPZ(AREACODE,ORGNAME,ORGCODE,ISSUE,
                INTIME,STATUS,KJDN,PDQJ,djbh,pdh,PDRQ,DWDM,DWMC
           ,jsfsdm ,jsfs,zjxzdm,zjxzmc,yslydm,yslymc ,ZCFLDM,ZCFLMC,YSXMDM,YSXMMC ,FKR,FKZH,FKKHYH,FKYHHH
           ,SKR,SKZH ,SKRKHYH,JE,ZPH,ZY,YSID)values(
                            '" + GetString(jb, "dwcode") + "','" + GetString(jb, "dwmc") + "','" + GetString(jb, "dwcode") + "','" + DateTime.Today.ToString("yyyyMM") + "'," +
                                "'" + DateTime.Today.ToString("yyyy/MM/dd") + "','0','" + GetString(jb, "kjdn") + "','" + GetString(jb, "pdqj") + "'," +
                                "'" + GetString(jb, "djbh") + "','" + GetString(jb, "pdh") + "','" + GetString(jb, "pdrq") + "','" + GetString(jb, "dwcode") + "'," +
                                "'" + GetString(jb, "dwmc") + "','" + GetString(jb, "jsfsdm") + "','" + GetString(jb, "jsfs") + "','" + GetString(jb, "zjxzdm") + "'," +
                                "'" + GetString(jb, "zjxzmc") + "','" + GetString(jb, "yslydm") + "','" + GetString(jb, "yslymc") + "','" + GetString(jb, "zcfldm") + "'," +
                                "'" + GetString(jb, "zcflmc") + "','" + GetString(jb, "ysxmdm") + "','" + GetString(jb, "ysxmmc") + "'," +
                                "'" + GetString(jb, "fkr") + "','" + GetString(jb, "fkzh") + "','" + GetString(jb, "fkkhyh") + "','" + GetString(jb, "fkyhhh") + "'," +
                                "'" + GetString(jb, "skr") + "','" + GetString(jb, "skzh") + "','" + GetString(jb, "skrkhyh") + "'," +
                                "'" + je + "','" + GetString(jb, "zph") + "','" + GetString(jb, "zy") + "','" + GetString(jb, "ysid") + "');";
                    }
                    sql += " end";
                    await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
                }
            }
            catch (Exception ex)
            {
                ver = ex.Message;
            }

            return ver;

        }
        public static async Task<(DataTable, int)> GetZFPZ(string org, string zclxmc, string zjxzmc, string start, string end, int page, int Size)
        {

            string sql = "select a.*,b.orgname as areaname,b.org from ZFPZ a left join Org b on left(a.AREACODE,9)=b.org where 1=1";
            if (org != "")
            {
                sql += " and AREACODE   like '" + org + "%'";
            }
            if (start != "")
            {
                sql += "  and PDRQ >= '" + start + "'";
            }
            if (end != "")
            {
                sql += "  and PDRQ <= '" + end + "'";
            }
            if (zclxmc != "")
            {
                sql += " and zcflmc like '" + zclxmc + "%'";
            }
            if (zjxzmc != "")
            {
                sql += " and zjxzmc like '" + zjxzmc + "%'";
            }

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "AREACODE", true, Size, page);
        }

        public static async Task<DataTable> GetZFPZInfo(string djbh,string org)
        {
            DataTable table = null;

            string sql = "select a.*,b.orgname as areaname,b.org as area from ZFPZ a " +
            "left join Org b on left(a.AREACODE,9)=b.org where djbh='" + djbh + "' and KJDN='" + DateTime.Today.Year + "' and ORGCODE='" + org+"';";
            //sql += "declare @zbid varchar(100);";
            //sql += "set @zbid=select zbid from ZB_VIEW_ZFPZ where djbh='" + djbh + "' and kjnd='" + DateTime.Today.Year + "';";
            //sql += "select * from ZB_VIEW_MXZB where zbid=@zbid";
            table= await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            if (table.Rows.Count ==0)
            {
                 sql = "select a.*,b.orgname as areaname,b.org as area from ZFPZ a " +
               "left join Org b on left(a.AREACODE,9)=b.org where djbh='" + djbh + "' and KJDN='" + (DateTime.Today.Year-1) + "' and ORGCODE='" + org + "';";
                //sql += "declare @zbid varchar(100);";
                //sql += "set @zbid=select zbid from ZB_VIEW_ZFPZ where djbh='" + djbh + "' and kjnd='" + DateTime.Today.Year + "';";
                //sql += "select * from ZB_VIEW_MXZB where zbid=@zbid";
                table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            }
            return table;
        }

        public static async Task<DataTable> GetZFPZInfobyid(string id)
        {

            string sql = "select a.*,b.orgname as areaname,b.org as area from ZFPZ a " +
            "left join Org b on left(a.AREACODE,9)=b.org where id=" + id + ";";
            //sql += "declare @zbid varchar(100);";
            //sql += "set @zbid=select zbid from ZB_VIEW_ZFPZ where djbh='" + djbh + "' and kjnd='" + DateTime.Today.Year + "';";
            //sql += "select * from ZB_VIEW_MXZB where zbid=@zbid";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZBInfo(string djbh, string org)
        {
            DataTable ta = new DataTable();
            string sql = "";
            sql += "declare @zbid varchar(100);declare @org varchar(50);";
            sql += "set @zbid=(select top 1 ysid from ZFPZ where djbh='" + djbh + "' and KJDN='" + DateTime.Today.Year + "');";
            sql += "set @org=(select top 1 areacode from ZFPZ where djbh='" + djbh + "' and KJDN='" + DateTime.Today.Year + "');";
            sql += @"select top 1 a.*,isnull(a.JE,0)-isnull(b.JE,0) as ye from Budget a left join 
(select ysid, sum(je) as je from ZFPZ group by YSID) b on a.YSID = b.YSID  where a.ysid=@zbid and areacode= @org";
            ta= await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            if (ta.Rows.Count == 0)
            {
                sql = "";
                sql += "declare @zbid varchar(100);declare @org varchar(50);";
                sql += "set @zbid=(select top 1 ysid from ZFPZ where djbh='" + djbh + "' and KJDN='" + (DateTime.Today.Year-1) + "');";
                sql += "set @org=(select top 1 areacode from ZFPZ where djbh='" + djbh + "' and KJDN='" + (DateTime.Today.Year-1) + "');";
                sql += @"select top 1 a.*,isnull(a.JE,0)-isnull(b.JE,0) as ye from Budget a left join 
(select ysid, sum(je) as je from ZFPZ group by YSID) b on a.YSID = b.YSID  where a.ysid=@zbid and areacode= @org";
            }
            return ta;
        }

        public static async Task<DataTable> GetZBInfoID(string id)
        {

            string sql = "";
            sql += "declare @zbid varchar(100);declare @org varchar(50);";
            sql += "set @zbid=(select top 1 ysid from ZFPZ where id=" + id + ";";
            sql += "set @org=(select top 1 areacode from ZFPZ where id=" + id + ";";
            sql += @"select top 1 a.*,isnull(a.JE,0)-isnull(b.JE,0) as ye from Budget a left join 
(select ysid, sum(je) as je from ZFPZ group by YSID) b on a.YSID = b.YSID  where a.ysid=@zbid and areacode= @org";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        #endregion

        #region “预算接口”


        public static async Task<string> ImportBudget(JArray ja)
        {
            string ver = "";
            try
            {
                if (ja.Count > 0)
                {
                    string sql = "begin ";
                    foreach (JObject jb in ja)
                    {
                        string je = "0";
                        if (GetString(jb, "je") != "")
                        {
                            je = GetString(jb, "je");
                        }
                        sql += @"delete from Budget where YSBH='" + GetString(jb, "id") + @"';INSERT INTO Budget(AREACODE ,ORGNAME,ORGCODE,ISSUE,
                            INTIME,STATUS,YSLX ,YSBH,YSFL,TJLX,TZH ,DWDM,DWMC,ZJXZDM,ZJXZMC,YSLYDM,YSLYMC,ZCFLDM,ZCFLMC ,JE,ZY) values(
                            '" + GetString(jb, "dwcode") + "','" + GetString(jb, "dwmc") + "','" + GetString(jb, "dwcode") + "','" + DateTime.Today.ToString("yyyyMM") + "'," +
                                "'" + DateTime.Today.ToString("yyyy/MM/dd") + "','0','" + GetString(jb, "yslx") + "','" + GetString(jb, "id") + "'," +
                                "'" + GetString(jb, "ysfl") + "','" + GetString(jb, "tjlx") + "','" + GetString(jb, "tzh") + "','" + GetString(jb, "dwcode") + "'," +
                                "'" + GetString(jb, "dwmc") + "','" + GetString(jb, "zjxzbm") + "','" + GetString(jb, "zjxzmc") + "','" + GetString(jb, "yslydm") + "'," +
                                "'" + GetString(jb, "yslymc") + "','" + GetString(jb, "zcfldm") + "','" + GetString(jb, "zcflmc") + "','" + je + "'," +
                                "'" + GetString(jb, "zy") + "');";
                    }
                    sql += " end";
                    await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
                }
            }
            catch (Exception ex)
            {
                ver = ex.Message;
            }

            return ver;

        }
        public static async Task<(DataTable, int)> Getbugets(string org, string month, int page, int Size)
        {
            int count = org.Length + 3;
            string s = @"
           select max(issue) from Budget where left(issue, 4) = '" + month + @"' and AREACODE like '" + org + "%'; ";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(s, null);
            string m = "";
            if (table.Rows.Count > 0)
            {
                m = table.Rows[0][0].ToString();
                if (m == "")
                {
                    m = DateTime.Today.ToString("yyyyMM");
                }
            }
            else
            {
                //m = DateTime.Today.AddMonths(-1).ToString("yyyyMM");
            }
            string sql = @"
select ID,c.org as AREACODE,b.orgName,b.org as ORGCODE,ISSUE ,INTIME ,STATUS ,YSLX,YSBH,YSFL,TJLX,TZH,DWDM
      , DWMC, ZJXZDM, ZJXZMC, YSLYDM, YSLYMC, ZCFLDM, ZCFLMC, JE, ZY, ZDR, ZD_RQ, SSR, SS_RQ, XDR
      , XD_RQ, a.YSID, JZSYRQ, SHR, SH_RQ, xmdm, xmmc, c.orgname as AREANAME,isnull(f.zc,0) as zc,je-ISNULL(zc,0) as ye,
convert(decimal(18,2),(case when isnull(je,0)=0 then 0 when f.zc=je then 100.00 else isnull(f.zc,0)/je end)) as zxl      
from Budget a
           left join Org b on a.AREACODE = b.org left join Org c on left(AREACODE, " + count + @") = c.org left join
(select e.org,sum(je) as zc,d.YSID from ZFPZ d left join org e on left(d.AREACODE," + count + ") = e.org where left(issue,4)='" + m.Substring(0, 4) + @"' group by e.org,d.ysid) f 
on b.org=f.org and f.YSID=a.YSID where 1 = 1";
            if (org != "")
            {
                sql += " and AREACODE   like '" + org + "%'";
            }
            if (month != "")
            {
                sql += "  and ISSUE ='" + m + "'";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "ID", true, Size, page);
        }

        public static async Task<DataTable> GetBudgetInfo(string zbid, string org)
        {


            string sql = @"select top 1 ID,c.org as AREACODE,b.orgName,b.org as ORGCODE,ISSUE ,INTIME ,STATUS ,YSLX,YSBH,YSFL,TJLX,TZH,DWDM
      , DWMC, ZJXZDM, ZJXZMC, YSLYDM, YSLYMC, ZCFLDM, ZCFLMC, JE, ZY, ZDR, ZD_RQ, SSR, SS_RQ, XDR
      , XD_RQ, a.YSID, JZSYRQ, SHR, SH_RQ, xmdm, xmmc, c.orgname as AREANAME,isnull(f.zc,0) as zc,je-ISNULL(zc,0) as ye from Budget a
           left join Org b on a.AREACODE = b.org left join Org c on left(AREACODE, 9) = c.org left join
(select e.org,sum(je) as zc,d.YSID from ZFPZ d left join org e on left(d.AREACODE, 9) = e.org group by e.org,d.ysid) f 
on b.org=f.org and f.YSID=a.YSID where a.ysid='" + zbid + "' and b.org='" + org + "' and issue='" + DateTime.Today.Year + "01';";
            //sql += "select * from ZFPZ where YSID='" + zbid + "';";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<DataTable> GetZFInfo(string zbid)
        {
            string sql = "select * from ZFPZ where YSID='" + zbid + "';";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region "资产"
        public static async Task<(DataTable, int)> GetAssets(string org, string type, string zcmc, string zcbm, string zclb, string zylx, string zcxz, string zt, string zkdm, string dwlx, string dwsx, string cyjg, decimal xzls, decimal xzle, int page, int pagesize)
        {
            string sql = "";
            if (org.Contains("320571"))
            {
                sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,f.tradearea,
 convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)/
 (case when a.zclx='固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl
from Asset a inner
join Org b on left(zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM where 1=1 ";
            }
            else
            {
                sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' then '闲置'  else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = ''  then '1'  else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,f.tradearea,
 convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)/
 (case when a.zclx='固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl
from Asset a left
join Org b on left(zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM where 1=1";
            }
            Logger _logger = LogManager.GetCurrentClassLogger();
            _logger.Info("1");
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                _logger.Info("1.1");
                foreach (string s1 in org.Split(","))
                {
                    _logger.Info("1.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "zcbm like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            _logger.Info("2");

            if (xzle > 0 & xzle >= xzls)
            {
                sql += @" and (convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100>=" + xzls + @" and  convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100<=" + xzle + ") ";
            }
            switch (type)
            {
                case "1":
                    sql += " and zclx='固定资产'";
                    break;
                case "2":
                    sql += " and zclx='资源资产'";
                    break;
            }
            _logger.Info("3");
            if (!string.IsNullOrEmpty(cyjg))
            {
                _logger.Info("3.1");
                string ss = "";
                string ss1 = "";
                foreach (string s1 in cyjg.Split(","))
                {
                    _logger.Info("3.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        if (s1 == "经营性资产")
                        {
                            ss += "'111 经营性资产',";
                            ss1 += "'经营性资产',";
                        }
                        else if (s1 == "非经营性资产")
                        {
                            ss += "'112 非经营性资产',";
                            ss1 += "'非经营性资产',";
                        }
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    ss1 = ss1.Substring(0, ss1.Length - 1);
                    sql += " and (cyjg in (" + ss1 + ") or FIELD3 in (" + ss + "))";
                }
            }
            _logger.Info("4");
            if (!string.IsNullOrEmpty(dwlx))
            {
                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            _logger.Info("5");
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                //foreach (string s1 in dwsx)
                //{
                //    switch (s1)
                //    {
                //        case "0":
                //            ss += "'非量化',";
                //            break;
                //        case "1":
                //            ss += "'量化',";
                //            break;
                //        case "2":
                //            ss += "'富民',";
                //            break;
                //        case "3":
                //            ss += "'镇属',";
                //            break;

                //    }
                //}
                //if (ss.Length > 0)
                //{
                //    ss = ss.Substring(0, ss.Length - 1);

                //    sql += " and (SUBSTRING(k.FIELD2,5,len(k.FIELD2)) in (" + ss + ") or k.FIELD2 in (" + ss + "))";
                //}
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and b.lh in(" + ss + ")";
                }
            }
            _logger.Info("6");
            if (!string.IsNullOrEmpty(zkdm))
            {

                string ss = "";
                foreach (string s1 in zkdm.Split(","))
                {
                    switch (s1)
                    {
                        case "1":
                            ss += "'01',";
                            //sql += " and zkdm='01'";
                            break;
                        case "2":
                            ss += "'07',";
                            //sql += " and zkdm='07'";
                            break;
                        case "3":
                            ss += "'06','98',";
                            //sql += " and zkdm in('06','98')";
                            break;
                        case "4":
                            ss += "'02','04','03','05',";
                            //sql += " and zkdm in('02','04','03','05')";
                            break;
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and zkdm in(" + ss + ")";
                }
                // sql += " and zkdm='"+zkdm+"'";
            }
            _logger.Info("7");
            if (zcmc != "")
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and zcbm like '%" + zcbm + "%'";
            }
            if (zclb != "")
            {
                sql += " and zclb like '%" + zclb + "%'";
            }
            if (zylx != "")
            {
                sql += " and zylx like '%" + zylx + "%'";
            }
            _logger.Info("8");
            if (!string.IsNullOrEmpty(zcxz))
            {

                string ss = "";
                foreach (string s1 in zcxz.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and a.zcxz in (" + ss + ")";
                }
            }
            _logger.Info("9");
            if (!string.IsNullOrEmpty(zt))
            {

                string ss = "";
                foreach (string s1 in zt.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and (case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) in (" + ss + ")";
                }
            }
            _logger.Info("10");
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "zcbm", true, pagesize, page);
        }

        public static async Task<DataTable> GetAssetsX(string org, string type, string zcmc, string zcbm, string zclb, string zylx, string zcxz, string zt, string zkdm, string dwlx, string dwsx, string cyjg, decimal xzls, decimal xzle, int page, int pagesize)
        {
            string sql = "";
            if (org.Contains("320571"))
            {
                sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,f.tradearea,
 convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)/
 (case when a.zclx='固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl
from Asset a inner
join Org b on left(zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM  where 1=1 ";
            }
            else
            {
                sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' then '闲置'  else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = ''  then '1'  else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,f.tradearea,
 convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)/
 (case when a.zclx='固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl
from Asset a left
join Org b on left(zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM where 1=1";
            }
            Logger _logger = LogManager.GetCurrentClassLogger();
            _logger.Info("1");
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                _logger.Info("1.1");
                foreach (string s1 in org.Split(","))
                {
                    _logger.Info("1.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "zcbm like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            _logger.Info("2");

            if (xzle > 0 & xzle >= xzls)
            {
                sql += @" and (convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100>=" + xzls + @" and  convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100<=" + xzle + ") ";
            }
            switch (type)
            {
                case "1":
                    sql += " and zclx='固定资产'";
                    break;
                case "2":
                    sql += " and zclx='资源资产'";
                    break;
            }
            _logger.Info("3");
            if (!string.IsNullOrEmpty(cyjg))
            {
                _logger.Info("3.1");
                string ss = "";
                string ss1 = "";
                foreach (string s1 in cyjg.Split(","))
                {
                    _logger.Info("3.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        if (s1 == "经营性资产")
                        {
                            ss += "'111 经营性资产',";
                            ss1 += "'经营性资产',";
                        }
                        else if (s1 == "非经营性资产")
                        {
                            ss += "'112 非经营性资产',";
                            ss1 += "'非经营性资产',";
                        }
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    ss1 = ss1.Substring(0, ss1.Length - 1);
                    sql += " and (cyjg in (" + ss1 + ") or FIELD3 in (" + ss + "))";
                }
            }
            _logger.Info("4");
            if (!string.IsNullOrEmpty(dwlx))
            {
                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            _logger.Info("5");
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                //foreach (string s1 in dwsx)
                //{
                //    switch (s1)
                //    {
                //        case "0":
                //            ss += "'非量化',";
                //            break;
                //        case "1":
                //            ss += "'量化',";
                //            break;
                //        case "2":
                //            ss += "'富民',";
                //            break;
                //        case "3":
                //            ss += "'镇属',";
                //            break;

                //    }
                //}
                //if (ss.Length > 0)
                //{
                //    ss = ss.Substring(0, ss.Length - 1);

                //    sql += " and (SUBSTRING(k.FIELD2,5,len(k.FIELD2)) in (" + ss + ") or k.FIELD2 in (" + ss + "))";
                //}
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and b.lh in(" + ss + ")";
                }
            }
            _logger.Info("6");
            if (!string.IsNullOrEmpty(zkdm))
            {

                string ss = "";
                foreach (string s1 in zkdm.Split(","))
                {
                    switch (s1)
                    {
                        case "1":
                            ss += "'01',";
                            //sql += " and zkdm='01'";
                            break;
                        case "2":
                            ss += "'07',";
                            //sql += " and zkdm='07'";
                            break;
                        case "3":
                            ss += "'06','98',";
                            //sql += " and zkdm in('06','98')";
                            break;
                        case "4":
                            ss += "'02','04','03','05',";
                            //sql += " and zkdm in('02','04','03','05')";
                            break;
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and zkdm in(" + ss + ")";
                }
                // sql += " and zkdm='"+zkdm+"'";
            }
            _logger.Info("7");
            if (zcmc != "")
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and zcbm like '%" + zcbm + "%'";
            }
            if (zclb != "")
            {
                sql += " and zclb like '%" + zclb + "%'";
            }
            if (zylx != "")
            {
                sql += " and zylx like '%" + zylx + "%'";
            }
            _logger.Info("8");
            if (!string.IsNullOrEmpty(zcxz))
            {

                string ss = "";
                foreach (string s1 in zcxz.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and a.zcxz in (" + ss + ")";
                }
            }
            _logger.Info("9");
            if (!string.IsNullOrEmpty(zt))
            {

                string ss = "";
                foreach (string s1 in zt.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and (case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) in (" + ss + ")";
                }
            }
            _logger.Info("10");
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }


        public static async Task<(DataTable, int)> GetAssetsDK(string org, string type, string zcmc, string zcbm, string zclb, string zylx, string zcxz, string zt, string zkdm, string dwlx, string dwsx, string cyjg, decimal xzls, decimal xzle,string dkmc,string lbdm,string sxmc, int page, int pagesize)
        {
            string sql = "";
            
                sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,f.tradearea,
 convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)/
 (case when a.zclx='固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl,l.dkmc,l.lbdm,l.lbmc,l.sxmc
from Asset a inner join ZC_DKZC m on a.zcbm=m.zcbm inner join ZC_DK l on m.dkid=l.listid inner
join Org b on left(a.zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(a.zcbm, 9) = c.org and c.isshow='1' left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM  where 1=1 ";
          
            Logger _logger = LogManager.GetCurrentClassLogger();
            _logger.Info("1");
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                _logger.Info("1.1");
                foreach (string s1 in org.Split(","))
                {
                    _logger.Info("1.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "a.zcbm like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dkmc))
            {
                sql += " and l.dkmc like '"+dkmc+"'";
            }
            if (!string.IsNullOrEmpty(lbdm))
            {
                //sql += " and l.lbdm='"+lbdm+"'";

                string ss = "";
                _logger.Info("1.1");
                foreach (string s1 in lbdm.Split(","))
                {
                    _logger.Info("1.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "l.lbdm= '" + s1 + "' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            _logger.Info("2");

            if (xzle > 0 & xzle >= xzls)
            {
                sql += @" and (convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100>=" + xzls + @" and  convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100<=" + xzle + ") ";
            }
            switch (type)
            {
                case "1":
                    sql += " and zclx='固定资产'";
                    break;
                case "2":
                    sql += " and zclx='资源资产'";
                    break;
            }
            _logger.Info("3");
            if (!string.IsNullOrEmpty(cyjg))
            {
                _logger.Info("3.1");
                string ss = "";
                string ss1 = "";
                foreach (string s1 in cyjg.Split(","))
                {
                    _logger.Info("3.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        if (s1 == "经营性资产")
                        {
                            ss += "'111 经营性资产',";
                            ss1 += "'经营性资产',";
                        }
                        else if (s1 == "非经营性资产")
                        {
                            ss += "'112 非经营性资产',";
                            ss1 += "'非经营性资产',";
                        }
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    ss1 = ss1.Substring(0, ss1.Length - 1);
                    sql += " and (cyjg in (" + ss1 + ") or FIELD3 in (" + ss + "))";
                }
            }
            _logger.Info("4");
            if (!string.IsNullOrEmpty(dwlx))
            {
                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            _logger.Info("5");
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                //foreach (string s1 in dwsx)
                //{
                //    switch (s1)
                //    {
                //        case "0":
                //            ss += "'非量化',";
                //            break;
                //        case "1":
                //            ss += "'量化',";
                //            break;
                //        case "2":
                //            ss += "'富民',";
                //            break;
                //        case "3":
                //            ss += "'镇属',";
                //            break;

                //    }
                //}
                //if (ss.Length > 0)
                //{
                //    ss = ss.Substring(0, ss.Length - 1);

                //    sql += " and (SUBSTRING(k.FIELD2,5,len(k.FIELD2)) in (" + ss + ") or k.FIELD2 in (" + ss + "))";
                //}
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and l.sxdm in(" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(sxmc))
            {

                string ss = "";
               
                foreach (string s1 in sxmc.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and l.sxdm in(" + ss + ")";
                }
            }
            _logger.Info("6");
            if (!string.IsNullOrEmpty(zkdm))
            {

                string ss = "";
                foreach (string s1 in zkdm.Split(","))
                {
                    switch (s1)
                    {
                        case "1":
                            ss += "'01',";
                            //sql += " and zkdm='01'";
                            break;
                        case "2":
                            ss += "'07',";
                            //sql += " and zkdm='07'";
                            break;
                        case "3":
                            ss += "'06','98',";
                            //sql += " and zkdm in('06','98')";
                            break;
                        case "4":
                            ss += "'02','04','03','05',";
                            //sql += " and zkdm in('02','04','03','05')";
                            break;
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and zkdm in(" + ss + ")";
                }
                // sql += " and zkdm='"+zkdm+"'";
            }
            _logger.Info("7");
            if (zcmc != "")
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and a.zcbm like '%" + zcbm + "%'";
            }
            if (zclb != "")
            {
                sql += " and zclb like '%" + zclb + "%'";
            }
            if (zylx != "")
            {
                sql += " and zylx like '%" + zylx + "%'";
            }
            _logger.Info("8");
            if (!string.IsNullOrEmpty(zcxz))
            {

                string ss = "";
                foreach (string s1 in zcxz.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and a.zcxz in (" + ss + ")";
                }
            }
            _logger.Info("9");
            if (!string.IsNullOrEmpty(zt))
            {

                string ss = "";
                foreach (string s1 in zt.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and (case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) in (" + ss + ")";
                }
            }
            _logger.Info("10");
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "zcbm", true, pagesize, page);
        }


        public static async Task<DataTable> GetAssetsDKX(string org, string type, string zcmc, string zcbm, string zclb, string zylx, string zcxz, string zt, string zkdm, string dwlx, string dwsx, string cyjg, decimal xzls, decimal xzle, string dkmc, string lbdm, string sxmc, int page, int pagesize)
        {
            string sql = "";

            sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,f.tradearea,
 convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)/
 (case when a.zclx='固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl,l.dkmc,l.lbdm,l.lbmc,l.sxmc
from Asset a inner join ZC_DKZC m on a.zcbm=m.zcbm inner join ZC_DK l on m.dkid=l.listid inner
join Org b on left(a.zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(a.zcbm, 9) = c.org and c.isshow='1' left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM where 1=1 ";

            Logger _logger = LogManager.GetCurrentClassLogger();
            _logger.Info("1");
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                _logger.Info("1.1");
                foreach (string s1 in org.Split(","))
                {
                    _logger.Info("1.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "a.zcbm like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dkmc))
            {
                sql += " and l.dkmc like '" + dkmc + "'";
            }
            if (!string.IsNullOrEmpty(lbdm))
            {
                //sql += " and l.lbdm='"+lbdm+"'";

                string ss = "";
                _logger.Info("1.1");
                foreach (string s1 in lbdm.Split(","))
                {
                    _logger.Info("1.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "l.lbdm= '" + s1 + "' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            _logger.Info("2");

            if (xzle > 0 & xzle >= xzls)
            {
                sql += @" and (convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100>=" + xzls + @" and  convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100<=" + xzle + ") ";
            }
            switch (type)
            {
                case "1":
                    sql += " and zclx='固定资产'";
                    break;
                case "2":
                    sql += " and zclx='资源资产'";
                    break;
            }
            _logger.Info("3");
            if (!string.IsNullOrEmpty(cyjg))
            {
                _logger.Info("3.1");
                string ss = "";
                string ss1 = "";
                foreach (string s1 in cyjg.Split(","))
                {
                    _logger.Info("3.2");
                    if (!string.IsNullOrEmpty(s1))
                    {
                        if (s1 == "经营性资产")
                        {
                            ss += "'111 经营性资产',";
                            ss1 += "'经营性资产',";
                        }
                        else if (s1 == "非经营性资产")
                        {
                            ss += "'112 非经营性资产',";
                            ss1 += "'非经营性资产',";
                        }
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    ss1 = ss1.Substring(0, ss1.Length - 1);
                    sql += " and (cyjg in (" + ss1 + ") or FIELD3 in (" + ss + "))";
                }
            }
            _logger.Info("4");
            if (!string.IsNullOrEmpty(dwlx))
            {
                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            _logger.Info("5");
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                //foreach (string s1 in dwsx)
                //{
                //    switch (s1)
                //    {
                //        case "0":
                //            ss += "'非量化',";
                //            break;
                //        case "1":
                //            ss += "'量化',";
                //            break;
                //        case "2":
                //            ss += "'富民',";
                //            break;
                //        case "3":
                //            ss += "'镇属',";
                //            break;

                //    }
                //}
                //if (ss.Length > 0)
                //{
                //    ss = ss.Substring(0, ss.Length - 1);

                //    sql += " and (SUBSTRING(k.FIELD2,5,len(k.FIELD2)) in (" + ss + ") or k.FIELD2 in (" + ss + "))";
                //}
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and l.sxdm in(" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(sxmc))
            {

                string ss = "";

                foreach (string s1 in sxmc.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and l.sxdm in(" + ss + ")";
                }
            }
            _logger.Info("6");
            if (!string.IsNullOrEmpty(zkdm))
            {

                string ss = "";
                foreach (string s1 in zkdm.Split(","))
                {
                    switch (s1)
                    {
                        case "1":
                            ss += "'01',";
                            //sql += " and zkdm='01'";
                            break;
                        case "2":
                            ss += "'07',";
                            //sql += " and zkdm='07'";
                            break;
                        case "3":
                            ss += "'06','98',";
                            //sql += " and zkdm in('06','98')";
                            break;
                        case "4":
                            ss += "'02','04','03','05',";
                            //sql += " and zkdm in('02','04','03','05')";
                            break;
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and zkdm in(" + ss + ")";
                }
                // sql += " and zkdm='"+zkdm+"'";
            }
            _logger.Info("7");
            if (zcmc != "")
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and a.zcbm like '%" + zcbm + "%'";
            }
            if (zclb != "")
            {
                sql += " and zclb like '%" + zclb + "%'";
            }
            if (zylx != "")
            {
                sql += " and zylx like '%" + zylx + "%'";
            }
            _logger.Info("8");
            if (!string.IsNullOrEmpty(zcxz))
            {

                string ss = "";
                foreach (string s1 in zcxz.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and a.zcxz in (" + ss + ")";
                }
            }
            _logger.Info("9");
            if (!string.IsNullOrEmpty(zt))
            {

                string ss = "";
                foreach (string s1 in zt.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and (case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) in (" + ss + ")";
                }
            }
            _logger.Info("10");
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<(DataTable, int)> GetAssetsx(string org, string type, string zcmc, string zcbm, string zclb, string zylx, string zcxz, string zt, string zkdm, string dwlx, string dwsx, string cyjg, decimal xzls, decimal xzle, int page, int pagesize)
        {
            string sql = "";
            Logger log = LogManager.GetCurrentClassLogger();
            sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,isnull(f.tradearea,0) as tradearea,
 convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)/
 (case when a.zclx='固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl
from Asset a left
join Org b on left(zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM where  zcbm not in (select zcbm from zc_dkzc) ";

            log.Info(sql);
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "zcbm like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }


            if (xzle > 0 & xzle >= xzls)
            {
                sql += @" and (convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100>=" + xzls + @" and  convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx = '固定资产' then(a.jzmj - isnull(f.tradearea, 0)) else (a.zymj - isnull(tradearea, 0)) end)/
        (case when a.zclx = '固定资产' then a.jzmj else a.zymj end) else 0 end))*100<=" + xzle + ") ";
            }
            switch (type)
            {
                case "1":
                    sql += " and zclx='固定资产'";
                    break;
                case "2":
                    sql += " and zclx='资源资产'";
                    break;
            }
            if (!string.IsNullOrEmpty(cyjg))
            {
                string ss = "";
                foreach (string s1 in cyjg.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and cyjg in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dwlx))
            {
                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                //foreach (string s1 in dwsx)
                //{
                //    switch (s1)
                //    {
                //        case "0":
                //            ss += "'非量化',";
                //            break;
                //        case "1":
                //            ss += "'量化',";
                //            break;
                //        case "2":
                //            ss += "'富民',";
                //            break;
                //        case "3":
                //            ss += "'镇属',";
                //            break;

                //    }
                //}
                //if (ss.Length > 0)
                //{
                //    ss = ss.Substring(0, ss.Length - 1);

                //    sql += " and (SUBSTRING(k.FIELD2,5,len(k.FIELD2)) in (" + ss + ") or k.FIELD2 in (" + ss + "))";
                //}
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and b.lh in(" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(zkdm))
            {
                if (!zkdm.Contains("0"))
                {
                    string ss = "";
                    foreach (string s1 in zkdm.Split(","))
                    {
                        switch (s1)
                        {
                            case "1":
                                ss += "'01',";
                                //sql += " and zkdm='01'";
                                break;
                            case "2":
                                ss += "'07',";
                                //sql += " and zkdm='07'";
                                break;
                            case "3":
                                ss += "'06','98',";
                                //sql += " and zkdm in('06','98')";
                                break;
                            case "4":
                                ss += "'02','04','03','05',";
                                //sql += " and zkdm in('02','04','03','05')";
                                break;
                        }
                    }
                    if (ss.Length > 0)
                    {
                        ss = ss.Substring(0, ss.Length - 1);
                        sql += " and zkdm in(" + ss + ")";
                    }
                }
                else
                {
                    string ss = "";
                    foreach (string s1 in zkdm.Split(","))
                    {
                        if (!string.IsNullOrEmpty(s1))
                        {
                            ss += "'" + s1 + "',";
                        }
                    }
                    if (ss.Length > 0)
                    {
                        ss = ss.Substring(0, ss.Length - 1);

                        sql += " and zkdm in(" + ss + ")";
                    }
                }
                // sql += " and zkdm='"+zkdm+"'";
            }
            if (!string.IsNullOrEmpty(zcmc))
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (!string.IsNullOrEmpty(zcbm))
            {
                zcbm = zcbm.Replace("[", "").Replace("]", "").Replace("\"","");
                //sql += " and zcbm like '%" + zcbm + "%'";
                string ss = "";
                foreach (string s1 in zcbm.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and zcbm not in (" + ss + ")";
                }
            }
            if (zclb != "")
            {
                sql += " and zclb like '%" + zclb + "%'";
            }
            if (zylx != "")
            {
                sql += " and zylx like '%" + zylx + "%'";
            }
            if (!string.IsNullOrEmpty(zcxz))
            {

                string ss = "";
                foreach (string s1 in zcxz.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and a.zcxz in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(zt))
            {

                string ss = "";
                foreach (string s1 in zt.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and (case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) in (" + ss + ")";
                }
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "zcbm", true, pagesize, page);
        }


        public static async Task<DataTable> GetAssetsByDM(string dms)
        {
            string sql = "";

            sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC,isnull(f.tradearea,0) as tradearea,
 convert(decimal(23,4), (case when (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)>0 then 
 (case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end)/
 (case when a.zclx='固定资产' then a.jzmj else a.zymj end) else 0 end))*100 as xzl
from Asset a left
join Org b on left(zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM where zcbm in (" + dms + ")";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetAssetsSum(string org, string type, string zcmc, string zcbm, string zclb, string zylx, string zcxz, string zt, string zkdm, string dwlx, string dwsx, string cyjg)
        {
            string wh = " and contractenddate > GETDATE()";
            if (zt == null)
            {
                wh = " and 1=2";
            }
            string ws = "";
            //if (!string.IsNullOrEmpty(dwlx))
            //{
            //    wh += " and m.ss='" + dwlx + "'";
            //}
            if (!string.IsNullOrEmpty(dwlx))
            {

                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    wh += " and m.ss in (" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(dwsx))
            {
                string lh = "";

                string ss = "";
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    wh += " and m.lh in(" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "areacode like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    wh += " and (" + ss + ")";
                }
            }
            //    if (!string.IsNullOrEmpty(dwsx))
            //{
            //    wh += " and m.lh='" + dwsx + "'";
            //}
            string sql = "";
            if (org.Contains("320571"))
            {
                sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,
isnull((select count(1) from Contract l inner join org m on l.areacode=m.org " + ws + @" where 1=1 " + wh + @"),0) as htsl,
isnull((select sum(ttlamt) from Contract l inner join org m on l.areacode=m.org " + ws + @" where 1=1 " + wh + @"),0) as htje,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC
from Asset a inner
join Org b on left(zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM where 1=1";
            }
            else
            {
                sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' then '闲置' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' then '1' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,
isnull((select count(1) from Contract l inner join org m on l.areacode=m.org " + ws + @" where areacode like '" + org + @"%' " + wh + @"),0) as htsl,
isnull((select sum(ttlamt) from Contract l inner join org m on l.areacode=m.org " + ws + @" where areacode like '" + org + @"%' " + wh + @"),0) as htje,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC
from Asset a left
join Org b on left(zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM where 1=1";
            }
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "zcbm like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }


            switch (type)
            {
                case "1":
                    sql += " and zclx='固定资产'";
                    break;
                case "2":
                    sql += " and zclx='资源资产'";
                    break;
            }
            if (!string.IsNullOrEmpty(cyjg))
            {
               
                string ss = "";
                string ss1 = "";
                foreach (string s1 in cyjg.Split(","))
                {
                   
                    if (!string.IsNullOrEmpty(s1))
                    {
                        if (s1 == "经营性资产")
                        {
                            ss += "'111 经营性资产',";
                            ss1 += "'经营性资产',";
                        }
                        else if (s1 == "非经营性资产")
                        {
                            ss += "'112 非经营性资产',";
                            ss1 += "'非经营性资产',";
                        }
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    ss1 = ss1.Substring(0, ss1.Length - 1);
                    sql += " and (cyjg in (" + ss1 + ") or FIELD3 in (" + ss + "))";
                }
            }
            if (!string.IsNullOrEmpty(dwlx))
            {
                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                //foreach (string s1 in dwsx)
                //{
                //    switch (s1)
                //    {
                //        case "0":
                //            ss += "'非量化',";
                //            break;
                //        case "1":
                //            ss += "'量化',";
                //            break;
                //        case "2":
                //            ss += "'富民',";
                //            break;
                //        case "3":
                //            ss += "'镇属',";
                //            break;

                //    }
                //}
                //if (ss.Length > 0)
                //{
                //    ss = ss.Substring(0, ss.Length - 1);

                //    sql += " and (SUBSTRING(k.FIELD2,5,len(k.FIELD2)) in (" + ss + ") or k.FIELD2 in (" + ss + "))";
                //}
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and b.lh in(" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(zkdm))
            {

                string ss = "";
                foreach (string s1 in zkdm.Split(","))
                {
                    switch (s1)
                    {
                        case "1":
                            ss += "'01',";
                            //sql += " and zkdm='01'";
                            break;
                        case "2":
                            ss += "'07',";
                            //sql += " and zkdm='07'";
                            break;
                        case "3":
                            ss += "'06','98',";
                            //sql += " and zkdm in('06','98')";
                            break;
                        case "4":
                            ss += "'02','04','03','05',";
                            //sql += " and zkdm in('02','04','03','05')";
                            break;
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and zkdm in(" + ss + ")";
                }
                // sql += " and zkdm='"+zkdm+"'";
            }
            if (zcmc != "")
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and zcbm like '%" + zcbm + "%'";
            }
            if (zclb != "")
            {
                sql += " and zclb like '%" + zclb + "%'";
            }
            if (zylx != "")
            {
                sql += " and zylx like '%" + zylx + "%'";
            }
            if (!string.IsNullOrEmpty(zcxz))
            {

                string ss = "";
                foreach (string s1 in zcxz.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and a.zcxz in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(zt))
            {

                string ss = "";
                foreach (string s1 in zt.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and (case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) in (" + ss + ")";
                }
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetAssetsSumDK(string org, string type, string zcmc, string zcbm, string zclb, string zylx, string zcxz, string zt, string zkdm, string dwlx, string dwsx, string cyjg,string dkmc,string lbdm,string sxmc)
        {
            string wh = " and contractenddate > GETDATE()";
            if (zt == null)
            {
                wh = " and 1=2";
            }
            string ws = "";
            //if (!string.IsNullOrEmpty(dwlx))
            //{
            //    wh += " and m.ss='" + dwlx + "'";
            //}
            if (!string.IsNullOrEmpty(dwlx))
            {

                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    wh += " and m.ss in (" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(dwsx))
            {
                string lh = "";

                string ss = "";
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    wh += " and m.lh in(" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "areacode like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    wh += " and (" + ss + ")";
                }
            }
            //    if (!string.IsNullOrEmpty(dwsx))
            //{
            //    wh += " and m.lh='" + dwsx + "'";
            //}
            string sql = "";
           
                sql = @"select distinct a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) as zt,
(case when a.zclx='固定资产' then (a.jzmj-isnull(f.tradearea,0)) else (a.zymj-isnull(tradearea,0)) end) as kymj,
isnull((select count(1) from Contract l inner join org m on l.areacode=m.org " + ws + @" where 1=1 " + wh + @"),0) as htsl,
isnull((select sum(ttlamt) from Contract l inner join org m on l.areacode=m.org " + ws + @" where 1=1 " + wh + @"),0) as htje,h.text as dwlx,j.text as dwsx,k.ZKDM,k.ZKMC
from Asset a inner join ZC_DKZC m on a.zcbm=m.zcbm inner join ZC_DK l on m.dkid=l.listid inner
join Org b on left(a.zcbm, 15) = b.org and b.isshow='1' left
join Org c on left(a.zcbm, 9) = c.org and c.isshow='1'
left
join (select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() group by assetcode) f
on a.zcbm = f.assetcode left join DicInfo h on b.lh=h.value and h.item='orgsx' left join DicInfo j on b.ss=j.value and j.item='orgfl' left join 
vw_fa_kp k on a.zcbm=k.GDDM  where 1=1";
           
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "a.zcbm like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(dkmc))
            {
                sql += " and l.dkmc like '"+dkmc+"'";
            }
            if (!string.IsNullOrEmpty(lbdm))
            {
                //sql += " and l.lbdm='"+lbdm+"'";

                string ss = "";
              
                foreach (string s1 in lbdm.Split(","))
                {
                  
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "l.lbdm= '" + s1 + "' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            switch (type)
            {
                case "1":
                    sql += " and zclx='固定资产'";
                    break;
                case "2":
                    sql += " and zclx='资源资产'";
                    break;
            }
            if (!string.IsNullOrEmpty(cyjg))
            {
                
                string ss = "";
                string ss1 = "";
                foreach (string s1 in cyjg.Split(","))
                {
                   
                    if (!string.IsNullOrEmpty(s1))
                    {
                        if (s1 == "经营性资产")
                        {
                            ss += "'111 经营性资产',";
                            ss1 += "'经营性资产',";
                        }
                        else if (s1 == "非经营性资产")
                        {
                            ss += "'112 非经营性资产',";
                            ss1 += "'非经营性资产',";
                        }
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    ss1 = ss1.Substring(0, ss1.Length - 1);
                    sql += " and (cyjg in (" + ss1 + ") or FIELD3 in (" + ss + "))";
                }
            }
            if (!string.IsNullOrEmpty(dwlx))
            {
                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                //foreach (string s1 in dwsx)
                //{
                //    switch (s1)
                //    {
                //        case "0":
                //            ss += "'非量化',";
                //            break;
                //        case "1":
                //            ss += "'量化',";
                //            break;
                //        case "2":
                //            ss += "'富民',";
                //            break;
                //        case "3":
                //            ss += "'镇属',";
                //            break;

                //    }
                //}
                //if (ss.Length > 0)
                //{
                //    ss = ss.Substring(0, ss.Length - 1);

                //    sql += " and (SUBSTRING(k.FIELD2,5,len(k.FIELD2)) in (" + ss + ") or k.FIELD2 in (" + ss + "))";
                //}
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and l.sxdm in(" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(sxmc))
            {

                string ss = "";

                foreach (string s1 in sxmc.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and l.sxdm in(" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(zkdm))
            {

                string ss = "";
                foreach (string s1 in zkdm.Split(","))
                {
                    switch (s1)
                    {
                        case "1":
                            ss += "'01',";
                            //sql += " and zkdm='01'";
                            break;
                        case "2":
                            ss += "'07',";
                            //sql += " and zkdm='07'";
                            break;
                        case "3":
                            ss += "'06','98',";
                            //sql += " and zkdm in('06','98')";
                            break;
                        case "4":
                            ss += "'02','04','03','05',";
                            //sql += " and zkdm in('02','04','03','05')";
                            break;
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and zkdm in(" + ss + ")";
                }
                // sql += " and zkdm='"+zkdm+"'";
            }
            if (zcmc != "")
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and a.zcbm like '%" + zcbm + "%'";
            }
            if (zclb != "")
            {
                sql += " and zclb like '%" + zclb + "%'";
            }
            if (zylx != "")
            {
                sql += " and zylx like '%" + zylx + "%'";
            }
            if (!string.IsNullOrEmpty(zcxz))
            {

                string ss = "";
                foreach (string s1 in zcxz.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and a.zcxz in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(zt))
            {

                string ss = "";
                foreach (string s1 in zt.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and (case when isnull(f.assetcode, '') = '' and k.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and k.ZKDM<>'01' then '3' else '2' end ) in (" + ss + ")";
                }
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetWXZC(string org, string zcmc, string zcbm, string zclb, string zcxz, string zkdm, string dwlx, string dwsx)
        {
            string sql = "select a.* from vw_fa_kp a inner join Org b on left(a.gddm,15)=b.org and b.isshow='1'  where lbdm like '30%'";
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "gddm like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dwlx))
            {

                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                //foreach (string s1 in dwsx.Split(","))
                //{
                //    switch (s1)
                //    {
                //        case "0":
                //            ss += "'非量化',";
                //            break;
                //        case "1":
                //            ss += "'量化',";
                //            break;
                //        case "2":
                //            ss += "'富民',";
                //            break;
                //        case "3":
                //            ss += "'镇属',";
                //            break;

                //    }
                //}
                //if (ss.Length > 0)
                //{
                //    ss = ss.Substring(0, ss.Length - 1);

                //    sql += " and (SUBSTRING(k.FIELD2,5,len(k.FIELD2)) in (" + ss + ") or k.FIELD2 in (" + ss + "))";
                //}
                foreach (string s1 in dwsx.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "'" + s1 + "',";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);

                    sql += " and b.lh in(" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(zkdm))
            {

                string ss = "";
                foreach (string s1 in zkdm.Split(","))
                {
                    switch (s1)
                    {
                        case "1":
                            ss += "'01',";
                            //sql += " and zkdm='01'";
                            break;
                        case "2":
                            ss += "'07',";
                            //sql += " and zkdm='07'";
                            break;
                        case "3":
                            ss += "'06','98',";
                            //sql += " and zkdm in('06','98')";
                            break;
                        case "4":
                            ss += "'02','04','03','05',";
                            //sql += " and zkdm in('02','04','03','05')";
                            break;
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and zkdm in(" + ss + ")";
                }
                // sql += " and zkdm='"+zkdm+"'";
            }


            if (zcmc != "")
            {
                sql += " and gdmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and gddm like  '%" + zcbm + "%'";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetAssetsInfo(string zcbm)
        {

            string sql = @"select a.*,b.org,b.orgName,c.org as area,c.orgName as areaname,
(case when isnull(f.assetcode, '') = '' and g.ZKDM='01' then '闲置' when isnull(f.assetcode, '') = '' and g.ZKDM<>'01' then '其他' else '出租' end ) as ztmc,
(case when isnull(f.assetcode, '') = '' and g.ZKDM='01' then '1' when isnull(f.assetcode, '') = '' and g.ZKDM<>'01' then '3' else '2' end ) as zt,
g.zkdm,g.zkmc
from Asset a left join Org b on left(zcbm,12)=b.org left join Org c on left(zcbm,9)=c.org
left join (select assetcode from Assets d inner join Contract e on d.projcode=e.projcode and contractenddate>GETDATE()) f
on a.zcbm=f.assetcode 
inner join
vw_fa_kp g on a.zcbm=g.GDDM  where zcbm='" + zcbm + @"';";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetHTMJ(string zcbm)
        {

            string sql = @"select assetcode,SUM(tradearea) as tradearea from VW_AssetArea a left join Contract b on a.projcode=b.projcode 
where b.contractenddate>=GETDATE() and assetcode='" + zcbm + "' group by assetcode";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetHTMJW(string zcbm)
        {

            string sql = @"select sum(tradearea) as tradearea from VW_AssetArea d inner join Contract e 
on d.projcode = e.projcode and contractenddate < GETDATE() where assetcode='" + zcbm + "' group by assetcode;";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        /// <summary>
        /// 合同信息
        /// </summary>
        /// <param name="zcbm"></param>
        /// <returns></returns>
        public static async Task<DataTable> GetAssetsContractInfo(string zcbm)
        {

            string sql = @"select distinct a.*,(case when isnull(b.mj,0)=0  then h.tradearea else b.mj end) as zlmj1,(case when contractenddate<GETDATE() then '历史合同' else '当前合同' end) as htzt,e.orgName,c.orgName as Areaname,(case when d.projcode is null then '否' else '是' end) as isplan
from Contract a 
                left join (select d.projcode,tradearea as mj from Assets d 
inner join Contract e on d.projcode = e.projcode and contractenddate > GETDATE()) b on a.projcode=b.projcode
				left join Org c on left(a.areacode,9)=left(c.org,9) and LEN(c.org)=9 left join Org e on a.areacode=e.org left
join (select top 1 projcode from ContractPlan) d on a.projcode = d.projcode left join 
VW_AssetArea h on h.projcode=a.projcode where a.projcode in (select projcode from Assets where assetcode = '" + zcbm + "')  and areacode is not null";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<(DataTable, int)> GetBuilding(string org, string area, string zcbm, string zcxz, string cjdw, string zcmc, string dwlx, string dwsx, int page, int pagesize)
        {

            string sql = "select a.*,b.org,b.orgName,c.org as area,c.orgName as areaname from Building a left join Org b on left(zcbm,12)=b.org  and b.isshow='1' left join Org c on left(zcbm,9)=c.org and c.isshow='1' where 1=1 ";
            //if (org != "")
            //{
            //    sql += " and  b.org like '" + org + "%'";
            //}
            if (!string.IsNullOrEmpty(org))
            {

                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += " b.org like '" + s1 + "%' or";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            if (area != "")
            {
                sql += " and c.org= '" + area + "'";
            }
            if (zcxz != "")
            {
                sql += " and zcxz = '" + zcxz + "'";
            }
            if (cjdw != "")
            {
                sql += " and cjdw like '%" + cjdw + "%'";
            }
            if (zcmc != "")
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and zcbm like '%" + zcbm + "%'";
            }
            if (!string.IsNullOrEmpty(dwlx))
            {

                string[] s = dwlx.Split(",");
                string ss = "";
                foreach (string s1 in s)
                {
                    ss += "'" + s1 + "',";
                }
                ss = ss.Substring(0, ss.Length - 1);
                sql += " and b.ss in (" + ss + ")";
            }

            if (!string.IsNullOrEmpty(dwsx))
            {
                string lh = "";

                string[] s = dwsx.Split(",");
                string ss = "";
                foreach (string s1 in s)
                {
                    ss += "'" + s1 + "',";
                }
                ss = ss.Substring(0, ss.Length - 1);
                sql += " and b.lh in(" + ss + ")";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "zcbm", true, pagesize, page);
        }

        public static async Task<DataTable> GetBuildingSum(string org, string area, string zcbm, string zcxz, string cjdw, string zcmc, string dwlx, string dwsx)
        {

            string sql = "select a.*,b.org,b.orgName,c.org as area,c.orgName as areaname from Building a left join Org b on left(zcbm,12)=b.org and b.isshow='1' left join Org c on left(zcbm,9)=c.org and c.isshow='1' where 1=1 ";
            if (!string.IsNullOrEmpty(org))
            {

                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += " b.org like '" + s1 + "%' or";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            if (area != "")
            {
                sql += " and c.org= '" + area + "'";
            }
            if (zcxz != "")
            {
                sql += " and zcxz = '" + zcxz + "'";
            }
            if (cjdw != "")
            {
                sql += " and cjdw like '%" + cjdw + "%'";
            }
            if (zcmc != "")
            {
                sql += " and zcmc like '%" + zcmc + "%'";
            }
            if (zcbm != "")
            {
                sql += " and zcbm like '%" + zcbm + "%'";
            }
            if (!string.IsNullOrEmpty(dwlx))
            {
                dwlx = dwlx.Replace("[", "").Replace("]", "").Replace("\"", "");
                string[] s = dwlx.Split(",");
                string ss = "";
                foreach (string s1 in s)
                {
                    ss += "'" + s1 + "',";
                }
                ss = ss.Substring(0, ss.Length - 1);
                sql += " and b.ss in (" + ss + ")";
            }

            if (!string.IsNullOrEmpty(dwsx))
            {
                string lh = "";
                dwsx = dwsx.Replace("[", "").Replace("]", "").Replace("\"", "");
                string[] s = dwsx.Split(",");
                string ss = "";
                foreach (string s1 in s)
                {
                    ss += "'" + s1 + "',";
                }
                ss = ss.Substring(0, ss.Length - 1);
                sql += " and b.lh in(" + ss + ")";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetBuildingByCode(string zcbm)
        {

            string sql = "select a.*,b.org,b.orgName,c.org as area,c.orgName as areaname from Building a left join Org b on left(zcbm,12)=b.org left join Org c on left(zcbm,9)=c.org where zcbm='" + zcbm + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region “预算预警”
        public static async Task<DataTable> GetYSS(string org)
        {

            string sql = @"declare @c int;
            set @c = LEN(" + org + @");declare @month varchar(6);
         set @month=(select max(issue) from Budget where LEFT(ISSUE, 4) = YEAR(GETDATE()));
             select org, b.orgName,b.color,convert(decimal(18,2),  (case when YSLYDM='02' then SUM(je)/10000 end)) as je,
convert(decimal(18,2), (case when YSLYDM<>'02' then isnull(SUM(je),0)/10000 end)) as zjje from Budget a right join Org b on left(a.AREACODE, @c + 3) = b.org and b.isshow='1' where ISSUE=@month and left(AREACODE, @c)= " + org + " group by org,b.orgName,b.color,YSLYDM;  ";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetYYYSS(string org)
        {

            //string sql = @"declare @c int;
            //set @c = LEN(" + org + @");

            //select org,b.orgName,b.color,SUM(JE) as je from ZFPZ a right join Org b on left(a.AREACODE,@c+3)=b.org where KJDN=YEAR(GETDATE()) and left(AREACODE,@c)=" + org + " and isnull(YSID,'')<>'' group by org,b.orgName,b.color;";
            string sql = @"declare @c int;declare @month varchar(6);
            set @c = LEN(" + org + @");
         set @month=(select max(issue) from Budget where LEFT(ISSUE, 4) = YEAR(GETDATE()));
             select c.org,c.orgname,c.color,convert(decimal(18,2), (case when c.je=0 then 0 else d.je/c.je end))*100 as je from
(select org,b.orgName,b.color,SUM(je) as je from Budget a right join Org b on left(a.AREACODE,@c+3)=b.org and b.isshow='1' where ISSUE=@month and left(AREACODE,@c)=" + org + @" group by org,b.orgName,b.color) c left join 
(select org,b.orgName,b.color,SUM(JE) as je from ZFPZ a right join Org b on left(a.AREACODE,@c+3)=b.org and b.isshow='1' where KJDN=YEAR(GETDATE()) and left(AREACODE,@c)=" + org + " and isnull(YSID,'')<>'' group by org,b.orgName,b.color) d on c.org=d.org;";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetSYYSS(string org)
        {

            string sql = @"declare @c int;declare @month varchar(6);
            set @c = LEN(" + org + @");
         set @month=(select max(issue) from Budget where LEFT(ISSUE, 4) = YEAR(GETDATE()));
             select c.org,c.orgname,c.color,convert(decimal(18,2), (c.je-d.je)/10000) as je from
(select org,b.orgName,b.color,SUM(je) as je from Budget a right join Org b on left(a.AREACODE,@c+3)=b.org and b.isshow='1' where ISSUE=@month and left(AREACODE,@c)=" + org + @" group by org,b.orgName,b.color) c left join 
(select org,b.orgName,b.color,SUM(JE) as je from ZFPZ a right join Org b on left(a.AREACODE,@c+3)=b.org and b.isshow='1' where KJDN=YEAR(GETDATE()) and left(AREACODE,@c)=" + org + " and isnull(YSID,'')<>'' group by org,b.orgName,b.color) d on c.org=d.org;";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region “合同预警”
        public static async Task<DataTable> Gethtsl(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @";
            set @c = len(@org);set @month=" + month + @" ;      
            select COUNT(1) as c from Contract where left(areacode, @c) = @org  ";
            if (endmonth == "")
            {
                sql += " and CONVERT(char(6),CONVERT(datetime, contractstartdate),112)= @month";
            }
            else
            {
                sql += " and (CONVERT(char(6),CONVERT(datetime, contractstartdate),112)>= @month and (CONVERT(char(6),CONVERT(datetime, contractstartdate),112)<= '" + endmonth + "')) ";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Gethtzje(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @";
            set @c = len(@org);set @month=" + month + @" ;      
            select convert(decimal(18,2), isnull(sum(ttlamt),0)/10000,0) as je from Contract where left(areacode,@c)=@org ";
            if (endmonth == "")
            {
                sql += " and CONVERT(char(6),CONVERT(datetime, contractstartdate),112)= @month";
            }
            else
            {
                sql += " and (CONVERT(char(6),CONVERT(datetime, contractstartdate),112)>= @month and (CONVERT(char(6),CONVERT(datetime, contractstartdate),112)<= '" + endmonth + "'))";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> Gethtysk(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @";
            set @c = len(@org);set @month=" + month + @" ;      
            select sum(jhskje)/10000 as je from ht_skjh a left join Contract b on a.HTBH=b.projcode  where left(areacode,@c)=@org ";
            if (endmonth == "")
            {
                sql += " and CONVERT(char(6),CONVERT(datetime, jhskrq),112)= @month";
            }
            else
            {
                sql += " and (CONVERT(char(6),CONVERT(datetime, jhskrq),112)>= @month and (CONVERT(char(6),CONVERT(datetime, jhskrq),112)<= '" + endmonth + "'))";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Gethtyqk(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @";
            set @c = len(@org);set @month=" + month + @" ;      
            select sum(jhskje)/10000-sum(isnull(skje,0))/10000 as qjje from ht_skjh a left join Contract b on a.HTBH=b.projcode
left join (select sum(CollectionAmount) as skje,projectCode,planid from ht_skxx group by projectCode,planid) c on a.HTBH=c.projectCode and a.ListID=c.planid  where left(areacode,@c)=@org ";
            if (endmonth == "")
            {
                sql += " and CONVERT(char(6),CONVERT(datetime, jhskrq),112)= @month";
            }
            else
            {
                sql += " and (CONVERT(char(6),CONVERT(datetime, jhskrq),112)>= @month and (CONVERT(char(6),CONVERT(datetime, jhskrq),112)<= '" + endmonth + "'))";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Gethtyje(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @";
            set @c = len(@org);set @month=" + month + @" ;      
            select CONVERT(char(4), MONTH(CONVERT(datetime,contractstartdate))) as month,convert(decimal(18,2), SUM(ttlamt)/10000) as je from Contract where left(areacode,@c)=@org and left(contractstartdate,4)=left(@month,4) group by MONTH(CONVERT(datetime,contractstartdate)); ";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Gethtysl(string org, string month, string endmonth)
        {

            string sql = "";
            if (endmonth == "")
            {
                sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @";
            set @c = len(@org);set @month=" + month + @";       
            select x.org,x.orgName,isnull(je,0) as je from Org x left join (
            select b.org,b.orgName,convert(decimal(18,2), sum(ttlamt)/10000) as je from Org b  left join contract a
			on left(a.areacode,@c+3)=b.org and b.isshow='1' where left(areacode,@c)=@org and len(areacode)>@c  
			and CONVERT(char(6),CONVERT(datetime, contractstartdate),112)= @month  and CONVERT(char(6),CONVERT(datetime, contractstartdate),112)= @month  group by b.org,b.orgName) y on x.org=y.org 
			where  left(x.org,@c)=@org and len(x.org)=@c+3";

            }
            else
            {
                sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @";
            set @c = len(@org);set @month=" + month + @";       
            select x.org,x.orgName,isnull(je,0) as je from Org x left join (
            select b.org,b.orgName,convert(decimal(18,2), sum(ttlamt)/10000) as je from Org b  left join contract a
			on left(a.areacode,@c+3)=b.org and b.isshow='1' where left(areacode,@c)=@org and len(areacode)>@c  
			and CONVERT(char(6),CONVERT(datetime, contractstartdate),112)= @month  and (CONVERT(char(6),CONVERT(datetime, contractstartdate),112)>= @month 
and (CONVERT(char(6),CONVERT(datetime, contractstartdate),112)<= '" + endmonth + @"'))  group by b.org,b.orgName) y on x.org=y.org 

            where  left(x.org,@c)=@org and len(x.org)=@c+3";
                // sql += " and (CONVERT(char(6),CONVERT(datetime, contractcreatedate),112)>= @month and (CONVERT(char(6),CONVERT(datetime, contractcreatedate),112)<= '" + endmonth + "'))";
            }
            // sql += " group by b.org,b.orgName";
            //and CONVERT(char(6),CONVERT(datetime,contractenddate),112)=@month group by b.org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Gethtcq(string org, string month, string endmonth)
        {

            string sql = @"";
            if (endmonth == "")
            {
                sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"  select x.org,x.orgName,isnull(c,0) as c from Org x left join (      
            select b.org,b.orgName,count(1) as c from Contract a left join Org b on left(a.areacode,@c+3)=b.org where left(areacode,@c)=@org  and len(areacode)>@c 
            and DATEDIFF(year,contractstartdate,contractenddate)>5  ";
                sql += @" and CONVERT(char(6),CONVERT(datetime, contractstartdate),112)= @month group by b.org,b.orgName) y on x.org=y.org 

            where left(x.org, @c)= @org and x.isshow='1' and len(x.org)= @c + 3";
            }
            else
            {
                sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"  select x.org,x.orgName,isnull(c,0) as c from Org x left join (      
            select b.org,b.orgName,count(1) as c from Contract a left join Org b on left(a.areacode,@c+3)=b.org  where left(areacode,@c)=@org  and len(areacode)>@c 
            and DATEDIFF(year,contractstartdate,contractenddate)>5  ";
                sql += " and (CONVERT(char(6),CONVERT(datetime, contractstartdate),112)>= @month and (CONVERT(char(6),CONVERT(datetime, contractstartdate),112)<= '" + endmonth + @"')) group by b.org,b.orgName) y on x.org=y.org 
            where left(x.org, @c)= @org and x.isshow='1' and len(x.org)= @c + 3";
            }

            //	and CONVERT(char(6),CONVERT(datetime,contractenddate),112)=@month group by b.org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> Gethtkp(string org)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
              set @c = len(@org); select x.org,x.orgName,isnull(skje,0) as skje,isnull(wskje,0) as wskje from Org x left join (      
            select * from (
            select org,orgname,convert(decimal(18,2), isnull(sum(b.CollectionAmount),0)/10000) as skje,
			convert(decimal(18,2), isnull(sum(d.payAmount),0)/10000) as wskje from Contract a left join ht_skxx b on a.projcode=b.projectCode and collectiontype='已对账' left join Org c 
            on left(a.areacode,@c+3)=c.org left join (select * from ht_skxx where collectiontype='已开票' and CollectionAmount=0) d on a.projcode=d.projectCode
			where left(areacode,@c)=@org and len(areacode)>@c group by org,orgname) e where e.skje>0 or wskje>0) y on x.org=y.org 
            where left(x.org, @c)= @org and x.isshow='1' and len(x.org)= @c + 3";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> Gezjyq(string org)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
                        set @c = len(@org);  select x.org,x.orgName,isnull(jhskje,0)-isnull(skje,0) as je from Org x left join (      
              select org,orgName,sum(isnull(b.jhskje,0))/10000 as jhskje,sum(isnull(c.CollectionAmount,0))/10000 as skje from contract a inner join HT_SKJH b on a.projcode=b.HTBH 
  left join ht_skxx c on a.projcode=c.projectCode and b.ListID=c.planid 
inner join Org d  on left(a.areacode,@c+3)=d.org where left(areacode,@c)=@org and len(areacode)>@c and b.jhskrq<GETDATE() group by org,orgName ) y on x.org=y.org 
            where left(x.org, @c)= @org  and x.isshow='1' and len(x.org)= @c + 3";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<(IEnumerable<dynamic>, int)> GetQJ(reportQj querry)
        {
            string str = "and   1=1 ";
            if (!string.IsNullOrEmpty(querry.contractcode) && querry.contractcode != "string")
            {
                str += "and contractcode like'%" + querry.contractcode + "%'";
            }
            if (!string.IsNullOrEmpty(querry.partya) && querry.partya != "string")
            {
                str += "and partya='" + querry.partya + "'";
            }
            if (!string.IsNullOrEmpty(querry.assetcode) && querry.assetcode != "string")
            {
                str += "and assetcode='" + querry.assetcode + "'";
            }
            if (!string.IsNullOrEmpty(querry.contractname) && querry.contractname != "string")
            {
                str += " and contractname like '%" + querry.contractname + "%'";
            }
            if (!string.IsNullOrEmpty(querry.gdmc) && querry.gdmc != "string")
            {
                str += " and gdmc='" + querry.gdmc + "'";
            }

            if (Convert.ToDecimal(querry.sqjje) <= Convert.ToDecimal(querry.eqjje) & Convert.ToDecimal(querry.eqjje) > 0)
            {

                str += " and (qzje>='" + querry.sqjje + "' and qzje<='" + querry.eqjje + "')";

            }
            if (Convert.ToDecimal(querry.sday) <= Convert.ToDecimal(querry.eday) & Convert.ToDecimal(querry.eday) > 0)
            {

                str += " and (days>='" + querry.sday + "' and days<='" + querry.eday + "')";

            }

            string sql = @"select * from vw_reportQj where areacode like  '" + querry.org +"%' "+ str;
            bool isAsc = true;
            querry.Sort = "asc";
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            // return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.PageSize, querry.Current);
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        public static async Task<(IEnumerable<dynamic>, int)> GetQJyq(reportQj querry)
        {
            string str = "and   1=1 ";
            if (!string.IsNullOrEmpty(querry.contractcode) && querry.contractcode != "string")
            {
                str += "and contractcode like'%" + querry.contractcode + "%'";
            }
            if (!string.IsNullOrEmpty(querry.partya) && querry.partya != "string")
            {
                str += "and partya='" + querry.partya + "'";
            }
            if (!string.IsNullOrEmpty(querry.assetcode) && querry.assetcode != "string")
            {
                str += "and assetcode='" + querry.assetcode + "'";
            }
            if (!string.IsNullOrEmpty(querry.contractname) && querry.contractname != "string")
            {
                str += " and contractname like '%" + querry.contractname + "%'";
            }
            if (!string.IsNullOrEmpty(querry.gdmc) && querry.gdmc != "string")
            {
                str += " and gdmc='" + querry.gdmc + "'";
            }
            if (!string.IsNullOrEmpty(querry.htzt) && querry.htzt != "string")
            {
                str += " and htzt='" + querry.htzt + "'";
            }
            if (!string.IsNullOrEmpty(querry.zjlc) && querry.zjlc != "string")
            {
                str += " and zjlc='" + querry.zjlc + "'";
            }
            if (!string.IsNullOrEmpty(querry.dwsx) && querry.dwsx != "string")
            {
                str += " and dwsx='" + querry.dwsx + "'";
            }

            if (Convert.ToDecimal(querry.sqjje) <= Convert.ToDecimal(querry.eqjje) & Convert.ToDecimal(querry.eqjje) > 0)
            {

                str += " and (qzje>='" + querry.sqjje + "' and qzje<='" + querry.eqjje + "')";

            }
            if (Convert.ToDecimal(querry.sday) <= Convert.ToDecimal(querry.eday) & Convert.ToDecimal(querry.eday) > 0)
            {

                str += " and (days>='" + querry.sday + "' and days<='" + querry.eday + "')";

            }

            string sql = @"select * from vw_ReportQjYQ where areacode like  '" + querry.org + "%' " + str;
            bool isAsc = true;
            querry.Sort = "asc";
            if (querry.Sort.ToUpper() != "ASC")
            {
                isAsc = false;
            }
            // return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.SortString, isAsc, querry.PageSize, querry.Current);
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.SortString, isAsc, querry.Size, querry.Current);

        }

        #endregion

        #region "收支预警"
        public static async Task<DataTable> GetYE(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
            select convert(decimal(18,2), isnull(sum(INCOME_TOTAL),0)/10000) -convert(decimal(18,2), isnull(sum(SERVE_INCOME),0)/10000) as ye from SZMXBDZ where ";
            if (endmonth == "")
            {
                sql += "ISSUE=@month and LEFT(AREACODE,@c)=@org";
            }
            else
            {
                sql += "ISSUE>=@month and issue<='" + endmonth + "' and LEFT(AREACODE,@c)=@org";
            }
            sql += " and AREACODE in(select org from Org where len(org)=15 and isshow='1')";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZSZ(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
            select convert(decimal(18,2), isnull(sum(INCOME_TOTAL),0)/10000) as zsr,convert(decimal(18,2), isnull(sum(SERVE_INCOME),0)/10000) as zzc from SZMXBDZ  where ";
            if (endmonth == "")
            {
                sql += "ISSUE=@month and left(AREACODE,@c)=@org";
            }
            else
            {
                sql += "ISSUE>=@month and issue<='" + endmonth + "' and LEFT(AREACODE,@c)=@org";
            }
            sql += " and AREACODE in(select org from Org where len(org)=15 and isshow='1')";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetSRPH(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
            select org,b.orgName, convert(decimal(18,2), isnull(sum(INCOME_TOTAL),0)/10000) as je,convert(decimal(18,2), isnull(sum(SERVE_INCOME),0)/10000) as zcje from SZMXBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ";
            if (endmonth == "")
            {
                sql += @"ISSUE=@month and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1') 

     group by org,b.orgName; ";
            }
            else
            {
                sql += @"ISSUE>=@month and issue<='" + endmonth + @"' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1') 

     group by org,b.orgName; ";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZCJG(string org, string month, string endmont)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
            select convert(decimal(18,2), isnull(sum(LABOUR_SERVE),0)/10000) as jy,convert(decimal(18,2), isnull(sum(AWARDING_TOTAL),0)/10000) as gl  from SZMXBDZ where ";
            if (endmont == "")
            {
                sql += "ISSUE=@month and left(AREACODE,@c)=@org";
            }
            else
            {
                sql += "ISSUE>=@month and issue<='" + endmont + "' and left(AREACODE,@c)=@org";
            }
            sql += " and AREACODE in(select org from Org where len(org)=15 and isshow='1')";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetYEPH(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
            select org,b.orgName,convert(decimal(18,2), isnull(sum(INCOME_TOTAL),0)/10000) -convert(decimal(18,2), isnull(sum(SERVE_INCOME),0)/10000) as je from SZMXBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ";
            if (endmonth == "")
            {
                sql += "ISSUE=@month and LEFT(AREACODE,@c)=@org and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1') group by org,b.orgName;";
            }
            else
            {
                sql += "ISSUE>=@month and issue<='" + endmonth + "' and LEFT(AREACODE,@c)=@org and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1') group by org,b.orgName;";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZCQS(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select right(ISSUE,2) as month,convert(decimal(18,2), isnull(sum(SERVE_INCOME),0)/10000) as zc,convert(decimal(18,2),isnull(sum(INCOME_TOTAL),0)/10000) as sr from SZMXBDZ where left(AREACODE,@c)=@org and left(ISSUE,4)=left(@month,4) and AREACODE in(select org from Org where len(org)=15 and isshow='1') group by right(ISSUE,2);";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetSRJG(string org, string month, string endmonth)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
            select convert(decimal(18,2), isnull(sum(OPERATING_TOTAL),0)/10000) as jy,
convert(decimal(18,2), isnull(sum(GROUP_ASSETS_RENT),0)/10000) as fb,convert(decimal(18,2), isnull(sum(ELECTRIC_BILL_INCOME),0)/10000) as bz,
convert(decimal(18,2), isnull(sum(WATER_BILL_INCOME),0)/10000) as qt,convert(decimal(18,2), isnull(sum(MATERIAL_SALE),0)/10000) as tz  from SZMXBDZ where ";
            if (endmonth == "")
            {
                sql += "ISSUE=@month and left(AREACODE,@c)=@org";
            }
            else
            {
                sql += "ISSUE>=@month and issue<='" + endmonth + "' and left(AREACODE,@c)=@org";
            }
            sql += " and AREACODE in(select org from Org where len(org)=15 and isshow='1')";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region "财务概况"
        public static async Task<DataTable> GetSRPHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org='" + org + @"'
            set @c = len(@org);set @month='" + month + @"'       
            select org,b.orgName, convert(decimal(18,2), isnull(sum(INCOME_TOTAL),0)/10000) as je from SZMXBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ISSUE like '" + month + @"%' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1')
	 group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZCPHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select org,b.orgName, convert(decimal(18,2), isnull(sum(SERVE_INCOME),0)/10000) as je from SZMXBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ISSUE like '" + month + @"%' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1')
	 group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetKMYEPHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select org,b.orgName, convert(decimal(18,2), isnull(sum(BORROWING_ASSETS_TOTAL+BORROWING_PROFIT_TOTAL+CREDIT_LIABILITIES_TOTAL+CREDIT_OWNER_TOTAL+CREDIT_PROFIT_TOTAL),0)/10000) as je from KMYEBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ISSUE like '" + month + @"%' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1')
	 group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZCFZPHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select org,b.orgName, convert(decimal(18,2), isnull(sum(ASSETS_TOTAL),0)/10000) as je
        from ZCFZBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ISSUE like '" + month + @"%' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1')
	 group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZCFZPHx1(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select org,b.orgName,
             convert(decimal(18,2), isnull(sum(LIABILITIES_TOTAL+OWNER_TOTAL),0)/10000) as je
           
        from ZCFZBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ISSUE like '" + month + @"%' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1')
	 group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZCFZPHx2(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select org,b.orgName,
           
            convert(decimal(18,2), isnull(sum(OWNER_TOTAL),0)/10000) as je
        from ZCFZBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ISSUE like '" + month + @"%' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1')
	 group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetSYFPPHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select org,b.orgName, convert(decimal(18,2), isnull(sum(INCOME_TOTAL),0)/10000) as je from SYFPBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ISSUE like '" + month + @"%' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1')
	 group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZWZQPHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select org,b.orgName, convert(decimal(18,2), isnull(sum(DEBT_BALANCE_TOTAL+DEBT_CURRENT_BALANCE),0)/10000) as je from ZWZQBDZ a inner join org b on left(a.AREACODE,@c+3)=org where ISSUE like '" + month + @"%' and left(AREACODE,@c)=@org  and len(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1')
	 group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region "主页"

        public static async Task<DataTable> GetOrgCount(string org)
        {
            string sql = @"select 
            (select count(1)  from org where len(org) = 9 and porg='" + org + @"') as town,(select count(1)  from org where len(org) = 12 and porg='" + org + @"') as cun,
            (select count(1) from org where len(org) = 15 and porg like '" + org + @"%') as org";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetzcyzPHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select org,b.orgName, sum(yz) as yz from Asset a left join org b on left(zcbm,@c+3)=b.org where left(zcbm,@c)=@org group by org,b.orgName;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetGDZCJG(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org='" + org + @"'
            set @c = len(@org); 
select isnull(sum(fw)/10000,0) fw,isnull(sum(qt)/10000,0) qt from (
select org,b.orgName,(case when zcxz='房屋及建筑物' then sum(yz) end) as fw,
(case when zcxz='其他类资产' then sum(yz) end) as qt
from Asset a left join org b on left(zcbm,@c+3)=b.org 
where left(zcbm,@c)=@org and zclx='固定资产' group by org,b.orgName,zcxz) c";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZYZCJG(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org='" + org + @"'
            set @c = len(@org);set @month='" + month + @"'  
select zclb as name,isnull(sum(zymj),0) as value
from Asset a left join org b on left(zcbm,@c+3)=b.org 
where left(zcbm,@c)=@org and zclb<>'固定资产' group by zclb";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GethtslPHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select * from(select org,(case when jc is not null then jc else orgName end) as orgName ,COUNT(1) c from Contract a left join org b on left(areacode,@c+3)=b.org 
            where left(areacode,@c)=@org and len(org)>@c and left(contractstartdate,4)=left(@month,4) and AREACODE in(select org from Org where len(org)=15  and isshow='1') group by org,(case when jc is not null then jc else orgName end)) c  order by org";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetSRJGx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
                
            select * from(select b.org,(case when jc is not null then jc else b.orgName end) as orgName,isnull(sum(OPERATING_TOTAL),0)/10000 as jy,isnull(sum(GROUP_ASSETS_RENT),0)/10000 as fb,
			isnull(sum(ELECTRIC_BILL_INCOME),0)/10000 as bz,isnull(sum(WATER_BILL_INCOME),0)/10000 as qt,isnull(sum(MATERIAL_SALE),0)/10000 as tz  
			from SZMXBDZ a inner join Org b on left(a.AREACODE,@c+3)=b.org  where  ";

            sql += "ISSUE=@month and left(AREACODE,@c)=@org AND LEN(org)=@c+3 and AREACODE in(select org from Org where len(org)=15  and isshow='1') group by org,(case when jc is not null then jc else b.orgName end)) c  order by org";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZCJGx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
            select * from(select b.org,(case when jc is not null then jc else b.orgName end) as orgName,isnull(sum(LABOUR_SERVE),0)/10000 as jy,isnull(sum(AWARDING_TOTAL),0)/10000 as gl  from SZMXBDZ  a inner join Org b on left(a.AREACODE,@c+3)=b.org where ";

            sql += "ISSUE=@month and left(AREACODE,@c)=@org AND LEN(org)=@c+3 and AREACODE in(select org from Org where len(org)=15 and isshow='1') group by org,(case when jc is not null then jc else b.orgName end)) c order by org";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetPZPH(string org, string month)
        {

            if (org.Contains("320508"))
            {
                string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
           select * from (select b.org,b.orgName,COUNT(1) as c,sum(je)/10000 as je from zfpz a 
			inner join Org b on left(a.AREACODE,@c+3)=b.org AND LEN(org)=@c+3 where  
            left(ISSUE,4)=left(@month,4) group by b.org, b.orgName) c order by org";

                return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            }
            else
            {
                string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
           select * from (select b.org,b.orgName,COUNT(1) as c,sum(je)/10000 as je from pzml a 
			inner join Org b on left(a.AREACODE,@c+3)=b.org AND LEN(org)=@c+3 where  
            left(ISSUE,4)=left(@month,4) group by b.org, b.orgName) c order by org";

                return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            }
        }

        public static async Task<DataTable> GethtjePHx(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select * from(select b.org,(case when jc is not null then jc else b.orgName end) as orgName,sum(ttlamt)/10000  as je from Contract a left join org b on left(areacode,@c+3)=b.org 
            where left(areacode,@c)=@org and len(org)>@c and left(contractstartdate,4)=left(@month,4) and AREACODE in(select org from Org where len(org)=15  and isshow='1') group by org,(case when jc is not null then jc else b.orgName end)) c order by org";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Getzsc(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select sum(INCOME_TOTAL)/10000 as INCOME_TOTAL,sum(SERVE_INCOME)/10000 as SPENDING_TOTAL from SZMXBDZ  where ISSUE=@month and left(AREACODE,@c)=@org and AREACODE in(select org from Org where len(org)=15  and isshow='1');";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Getxz(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select COUNT(1) as xz from Asset where zcxz='经营性资产' and zcbm not in(select assetcode from Assets);";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Getjg(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select 0 as c;";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Getdqht(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select count(1) as c from Contract where contractcreatedate<getdate() and left(contractcreatedate,4)=YEAR(GETDATE());";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Getzccb(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select count(1) as c from ZFPZ where PDQJ=@month and JE>100000";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Getccht(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select count(1) as c from Contract where DATEDIFF(YEAR,CONVERT(datetime, contractstartdate), CONVERT(datetime, contractcreatedate))>5 and left(contractcreatedate,4)=YEAR(GETDATE());";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Getcys(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
             select 0 as c";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> Getzjwdq(string org, string month)
        {

            string sql = @"declare @c int;declare @org varchar(50);declare @month varchar(6); set @org=" + org + @"
            set @c = len(@org);set @month=" + month + @"       
            select 0 as c";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region “凭证”
        public static async Task<(DataTable, int)> GetPZML(string org, string month, string endmont, string sflh, string ss, int page, int Size)
        {

            string sql = @"select ID ,c.org as AREACODE,b.ORGNAME,b.org as ORGCODE,ISSUE,INTIME,STATUS,gsdm,zth,kjqj,pzlx,pzh,pzrq
, zdr, shr, zjr, zd_rq, sh_rq, zj_rq, zy, je, IDPZH, c.orgname as areaname from pzml a inner
join Org b on a.AREACODE = b.org and b.isshow='1'
inner join Org c on left(a.AREACODE, 9) = c.org where 1=1 ";
            //            string sql = @"SELECT ISSUE as kjqj,VOUCHER_FONT as pzh,SUMMARY as zy,REPLACE(VOUCHER_DATE,'-','') as pzrq,
            //(case when DEBIT_AMOUNT > 0 then DEBIT_AMOUNT else CREDIT_AMOUNT end) as je,ATTACHMENT_UUID as fjs,'' as zdr
            //  FROM PZ where 1=1";
            if (org != "")
            {
                sql += " and AREACODE   like '" + org + "%'";
            }
            if (month != "" && string.IsNullOrEmpty(endmont))
            {
                sql += "  and kjqj=  '" + month + "'";
            }
            if (!string.IsNullOrEmpty(endmont))
            {
                sql += " and (kjqj>='" + month + "' and kjqj<='" + endmont + "')";
            }
            if (!string.IsNullOrEmpty(sflh))
            {
                sql += " and b.lh='" + sflh + "'";
            }
            if (!string.IsNullOrEmpty(ss))
            {
                sql += " and b.ss='" + ss + "'";
            }
            //if (month != "")
            //{
            //    sql += "  and ISSUE like  '" + month + "%'";
            //}
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "pzh", true, Size, page);
        }

        public static async Task<DataTable> GetPZMLInfo(string idpzh)
        {


            string sql = @"select ID ,c.org as AREACODE,b.ORGNAME,b.org as ORGCODE,ISSUE,INTIME,STATUS,gsdm,zth,kjqj,pzlx,pzh,pzrq
      , zdr, shr, zjr, zd_rq, sh_rq, zj_rq, zy, je, IDPZH, c.orgname as areaname from pzml a inner
                                                                                 join Org b on a.AREACODE = b.org

inner
                                                                                 join Org c on left(a.AREACODE, 9) = c.org where idpzh='" + idpzh + "'";
            // sql += "select * from pznr where idpzh='" + idpzh + "';";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetPZNRInfo(string idpzh)
        {

            //string sql = "select * from pzml where idpzh='" + idpzh + "';";
            string sql = @"select flh,zy,kmdm+''+ isnull(kmmc,'') as km,(case when jdbz='借' then je end) as jje,
(case when jdbz = '贷' then je end) as dje from pznr where idpzh='" + idpzh + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<(DataTable, int)> GetPZ(string org, string month, int page, int Size)
        {

            //string sql = "select * from pzml where idpzh='" + idpzh + "';";
            string sql = @"SELECT ID,c.org as AREACODE,b.orgName,b.org as ORGCODE,ISSUE,INTIME,STATUS,VOUCHER_DATE,VOUCHER_FONT,SUMMARY,
SUBJECT,SUBJECTNAME,DEBIT_AMOUNT,CREDIT_AMOUNT,ATTACHMENT_UUID,c.orgName as areaname
  FROM PZ a inner join Org b on a.AREACODE = b.org and b.isshow='1'

      inner join Org c on left(a.AREACODE,9)= c.org where 1=1 ";
            if (org != "")
            {
                sql += " and AREACODE   like '" + org + "%'";
            }
            if (month != "")
            {
                sql += "  and ISSUE like '" + month + "%'";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "VOUCHER_FONT", true, Size, page);
        }
        #endregion

        #region "合同"
        public static async Task<(DataTable, int)> GetContract(string org, string start, string end, string contractcode, string contractname, string htzt, string sfskjh, string yhtbh, string dwlx, string dwsx, int page, int pagesize)
        {
            //            string sql = @"select a.*,b.orgname,b.org,c.org as area,c.orgname as areaname,
            //(case when d.projcode is null then '否' else '是' end) as isplan,isnull(e.dzje, 0) as dzje,(a.ttlamt - isnull(e.dzje, 0)) as wdzje,(case when isnull(f.zlmj,0)=0 and isnull(j.mj,0)=0 then h.mj when isnull(f.zlmj,0)=0 and isnull(j.mj,0)<>0 then j.mj else f.zlmj end) zlmj,
            //(case when contractenddate<getdate() then '已执行完' else '正在执行' end) as htzt
            //from Contract a left
            //join Org b on left(a.areacode, 12) = b.org and b.isshow='1' left
            //join Org c on left(a.areacode, 9) = c.org and c.isshow='1'
            //left
            //join (select distinct projcode from ContractPlan) d on a.projcode = d.projcode left
            //join
            //(select projcode,sum(isnull(dzje,0)) as dzje from ContractMoney group by projcode) e on a.projcode = e.projcode
            //left join
            //(select d.projcode,sum((case when tradearea=0 then m.mj else d.tradearea end)) as zlmj from Assets d left join SendAssets m on d.assetcode=m.zcbm 
            //inner join Contract e on d.projcode = e.projcode and contractenddate > GETDATE() group by d.projcode) f on a.projcode=f.projcode
            //left join 
            //(select projcode,sum(mj) as mj from AssetP g inner join SendAssets h on g.assetid='096'+SUBSTRING(h.assetid,0,CHARINDEX('-',h.assetid)) and LEFT(g.assetid,3)<>'096' group by projcode) j on j.projcode=a.projcode left join
            //(select projcode,sum(mj) as mj from AssetP g inner join SendAssets h on g.assetid=h.Assetid and LEFT(g.assetid,3)='096' group by projcode) h on h.projcode=a.projcode  where areacode like '" + org + "%'";
            string sql = @"select a.projcode,areacode,contractfrom,contractcode ,contractname ,partya ,partyb,contractstartdate,contractenddate ,contractcreatedate,ttlamt ,firstrent
           ,acctname ,acctnumb ,accbankname,mobilephone,dutyperson,contractperson ,contactmobile ,auctionusertype ,auctionusernumber,bank ,bankaccount,auctiondate,operatorname
           ,operatorcardid,operatormobile,yhtbh ,bz ,ywyf ,yzj,jylx,htsm,skfs,zxkssj,zxjssj,zxqx,zlkssj
           ,zljssj,zlq,skgz,wyxz ,ysdf,kplx ,sssq,sssqbm,qtsm,b.orgname,b.org,c.org as area,c.orgname as areaname,
(case when d.projcode is null then '否' else '是' end) as isplan,
isnull(e.dzje, 0) as dzje,h.mj as zlmj,
(case when contractenddate<getdate() then '已执行完' else '正在执行' end) as htzt
from Contract a left
join Org b on left(a.areacode, 15) = b.org and b.isshow='1' left
join Org c on left(a.areacode, 9) = c.org and c.isshow='1'
left
join (select distinct projcode from ContractPlan) d on a.projcode = d.projcode left
join
(select projcode,sum(isnull(dzje,0)) as dzje from ContractMoney group by projcode) e on a.projcode = e.projcode
left join (select projcode,sum(tradearea) as mj from VW_AssetArea group by projcode) h on h.projcode=a.projcode inner join Org i on a.areacode=i.org  where 1=1";

            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "areacode like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    sql += " and (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
            {
                sql += " and (contractstartdate>='" + start + "' and contractstartdate<='" + end + "')";
            }
            if (!string.IsNullOrEmpty(contractcode))
            {
                sql += " and contractcode like '%" + contractcode + "%'";
            }
            if (!string.IsNullOrEmpty(contractname))
            {
                sql += " and contractname like '%" + contractname + "%'";
            }
            if (!string.IsNullOrEmpty(htzt) && htzt != "on")
            {
                if (htzt == "1")
                {
                    sql += " and contractenddate >= GETDATE()";
                }
                else
                {
                    sql += " and contractenddate< GETDATE()";
                }
            }
            //if (htzt == "on")
            //{ }
            if (!string.IsNullOrEmpty(sfskjh))
            {
                //sql += " and (case when d.projcode is null then '否' else '是' end)='" + sfskjh + "'";

                string ss = "";
                foreach (string s1 in sfskjh.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += "  and (case when d.projcode is null then '否' else '是' end) in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(yhtbh))
            {
                sql += " and yhtbh like '%" + yhtbh + "%'";
            }
            if (!string.IsNullOrEmpty(dwlx))
            {

                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                foreach (string s1 in dwsx.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.lh in (" + ss + ")";
                }
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "contractcreatedate", false, pagesize, page);
        }

        public static async Task<DataTable> GetContractSum(string org, string start, string end, string contractcode, string contractname, string htzt, string sfskjh, string yhtbh, string dwlx, string dwsx)
        {
            string ht = "";
            if (!string.IsNullOrEmpty(htzt) && htzt != "on")
            {
                if (htzt == "1")
                {
                    ht = " and contractenddate>= GETDATE()";
                }
                else
                {
                    ht = " and contractenddate< GETDATE()";
                }
            }
            if (!string.IsNullOrEmpty(contractcode))
            {
                ht += " and contractcode like '%" + contractcode + "%'";
            }
            if (!string.IsNullOrEmpty(contractname))
            {
                ht += " and contractname like '%" + contractname + "%'";
            }
            if (!string.IsNullOrEmpty(yhtbh))
            {
                ht += " and yhtbh like '%" + yhtbh + "%'";
            }
            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "areacode like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    ht += " and (" + ss + ")";
                }
            }
            string sql = @"select isnull(sum(isnull(ttlamt,0)),0) as ttlamt,
isnull(sum(isnull(j.mj,0)),0) as zlmj,
isnull((select count(1) from (select distinct assetcode from Assets x left join Contract y on x.projcode=y.projcode inner join Asset at on x.assetcode=at.zcbm  where 1=1 " + ht + @" ) z),0) as zcsl,
isnull((select SUM(yz) from (select distinct zcbm,yz from Asset x inner join Assets y on x.zcbm=y.assetcode inner join Contract r on y.projcode=r.projcode  where 1=1 " + ht + @") t),0) as zcyz
from Contract a left
join Org b on left(a.areacode, 15) = b.org and b.isshow='1' left
join Org c on left(a.areacode, 9) = c.org and c.isshow='1'
left
join (select distinct projcode from ContractPlan) d on a.projcode = d.projcode left
join
(select projcode,sum(isnull(dzje,0)) as dzje from ContractMoney group by projcode) e on a.projcode = e.projcode
left join
(select projcode,tradearea as mj from VW_AssetArea) j on j.projcode=a.projcode inner join Org i on a.areacode=i.org
where areacode like '" + org + @"%' ";

            if (!string.IsNullOrEmpty(org))
            {
                string ss = "";
                foreach (string s1 in org.Split(","))
                {
                    if (!string.IsNullOrEmpty(s1))
                    {
                        ss += "areacode like '" + s1 + "%' or ";
                    }
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 3);
                    ht += " and (" + ss + ")";
                }
            }

            if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
            {
                sql += " and (contractstartdate>='" + start + "' and contractstartdate<='" + end + "')";
            }
            if (!string.IsNullOrEmpty(contractcode))
            {
                sql += " and contractcode like '%" + contractcode + "%'";
            }
            if (!string.IsNullOrEmpty(contractname))
            {
                sql += " and contractname like '%" + contractname + "%'";
            }
            if (!string.IsNullOrEmpty(htzt) && htzt != "on")
            {
                if (htzt == "1")
                {
                    sql += " and contractenddate >= GETDATE()";
                }
                else
                {
                    sql += " and contractenddate< GETDATE()";
                }
            }
            if (!string.IsNullOrEmpty(sfskjh))
            {
                string ss = "";
                foreach (string s1 in sfskjh.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += "  and (case when d.projcode is null then '否' else '是' end) in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(yhtbh))
            {
                sql += " and yhtbh like '%" + yhtbh + "%'";
            }
            if (!string.IsNullOrEmpty(dwlx))
            {

                string ss = "";
                foreach (string s1 in dwlx.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.ss in (" + ss + ")";
                }
            }
            if (!string.IsNullOrEmpty(dwsx))
            {

                string ss = "";
                foreach (string s1 in dwsx.Split(","))
                {
                    ss += "'" + s1 + "',";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                    sql += " and b.lh in (" + ss + ")";
                }
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetContractInfo(string projectCode)
        {

            string sql = @"select distinct a.*,b.orgname,b.org,c.org as area,c.orgname as areaname,
(case when d.projcode is null then '否' else '是' end) as isplan,isnull(e.dzje, 0) as dzje,(a.ttlamt - isnull(e.dzje, 0)) as wdzje,f.projname
from Contract a left
join Org b on left(a.areacode, 12) = b.org left
join Org c on left(a.areacode, 9) = c.org
left
join (select top 1 projcode from ContractPlan) d on a.projcode = d.projcode left
join
(select projcode,sum(dzje) as dzje from ContractMoney group by projcode) e on a.projcode = e.projcode
left join Project f on a.projcode=f.projcode where isnull(a.projcode,'')<>'' and a.projcode='" + projectCode + "'";
            //sql += "select * from Assets  where projcode='" + projectCode + "';";
            //sql += "select * from ContractFile where projcode='" + projectCode + "'";
            //sql += "select * from ContractPlan where projcode='" + projectCode + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetContractAssetsInfo(string projectCode)
        {

            //string sql = "select * from Contract where projcode='" + projectCode + "'";
            //            string sql = @"select a.projcode,a.assetcode,(case when a.assetclass='1' then '经营性资产' when a.assetclass='2' then '资源性资产' 
            //when a.assetclass='3' then '非经营性资产（经营性部分）' end ) as assetclass,a.assettransprice,a.priceunit,
            //(case when isnull(a.tradearea,0)=0 and isnull(j.mj,0)=0 then h.mj when isnull(a.tradearea,0)=0 and isnull(j.mj,0)<>0 then j.mj else a.tradearea end) as tradearea,
            //(case when a.tradeunit='1' then '亩' when a.tradeunit='2' then '平方米' else a.tradeunit end) as tradeunit,
            //b.zcmc,b.zlwz,zclb,c.orgName,d.orgName as areaname,zclx,zcxz,(case when b.zclx='固定资产' then b.jzmj else zymj end) as mj,
            //isnull((case when b.zclx='固定资产' then b.jzmj else zymj end),0)-(case when isnull(a.tradearea,0)=0 and isnull(j.mj,0)=0 then h.mj when isnull(a.tradearea,0)=0 and isnull(j.mj,0)<>0 then j.mj else a.tradearea end) as kymj,
            //zjlx,tdzh,fczh,yz
            //from Assets a left join Asset b on a.assetcode=b.zcbm 
            //left join Org c on LEFT(a.assetcode,12)=c.org left join Org d on LEFT(a.assetcode,9)=d.org
            //left join 
            //(select projcode,sum(mj) as mj from AssetP g inner join SendAssets h on g.assetid='096'+SUBSTRING(h.assetid,0,CHARINDEX('-',h.assetid)) group by projcode) j on j.projcode=a.projcode left join
            //(select projcode,sum(mj) as mj from AssetP g inner join SendAssets h on g.assetid=h.Assetid and LEFT(g.assetid,3)='096' group by projcode) h on h.projcode=a.projcode
            //  where a.projcode='" + projectCode + "';";

            string sql = @"select a.projcode,a.assetcode,(case when a.assetclass='1' then '经营性资产' when a.assetclass='2' then '资源性资产' 
when a.assetclass='3' then '非经营性资产（经营性部分）' end ) as assetclass,a.assettransprice,a.priceunit,
h.mj as tradearea,
(case when a.tradeunit='1' then '亩' when a.tradeunit='2' then '平方米' else a.tradeunit end) as tradeunit,
b.zcmc,b.zlwz,zclb,c.orgName,d.orgName as areaname,zclx,zcxz,(case when b.zclx='固定资产' then b.jzmj else zymj end) as mj,
isnull((case when b.zclx='固定资产' then b.jzmj else zymj end),0)-h.mj as kymj,
zjlx,tdzh,fczh,yz
from Assets a left join Asset b on a.assetcode=b.zcbm 
left join Org c on LEFT(a.assetcode,12)=c.org left join Org d on LEFT(a.assetcode,9)=d.org
left join 
(select projcode,sum(tradearea) as mj from VW_AssetArea group by projcode) h on h.projcode=a.projcode
  where a.projcode='" + projectCode + "';";
            //sql += "select * from ContractFile where projcode='" + projectCode + "'";
            //sql += "select * from ContractPlan where projcode='" + projectCode + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetContractAssetsInfo1(string projectCode)
        {

            //string sql = "select * from Contract where projcode='" + projectCode + "'";
            string sql = @"select a.projcode,a.assetcode,(case when a.assetclass='1' then '经营性资产' when a.assetclass='2' then '资源性资产' 
when a.assetclass='3' then '非经营性资产（经营性部分）' end ) as assetclass,a.assettransprice,a.priceunit,a.tradearea,
(case when a.tradeunit='1' then '亩' when a.tradeunit='2' then '平方米' else a.tradeunit end) as tradeunit,
b.zcmc,b.zlwz,zclb,c.orgName,d.orgName as areaname,zclx,zcxz,(case when b.zclx='固定资产' then b.jzmj else zymj end) as mj,
isnull((case when b.zclx='固定资产' then b.jzmj else zymj end),0)-a.tradearea as kymj,
zjlx,tdzh,fczh,yz,(case when len( rtrim(assetid))>4 then  '100'+SUBSTRING(assetid,4,len(rtrim(assetid))-3) else '' end) as assetid
from Assets a left join Asset b on a.assetcode=b.zcbm 
left join Org c on LEFT(a.assetcode,12)=c.org left join Org d on LEFT(a.assetcode,9)=d.org  where a.projcode='" + projectCode + "';";
            //sql += "select * from ContractFile where projcode='" + projectCode + "'";
            //sql += "select * from ContractPlan where projcode='" + projectCode + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetContractFileInfo(string projectCode)
        {

            //string sql = "select * from Contract where projcode='" + projectCode + "'";
            //string sql = "select * from Assets  where projcode='" + projectCode + "';";
            string sql = "select * from ContractFile where projcode='" + projectCode + "'";
            //sql += "select * from ContractPlan where projcode='" + projectCode + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task UpdateContract(string prjcode, string yhtbh, string bz)
        {
            string sql = "update Contract set yhtbh='" + yhtbh + "',bz='" + bz + "' where projcode='" + prjcode + "'";
            //sql += "select * from ContractPlan where projcode='" + projectCode + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
        }
        public static async Task<DataTable> GetContractPlan(string projectCode)
        {

            //string sql = "select * from Contract where projcode='" + projectCode + "'";
            //string sql = "select * from Assets  where projcode='" + projectCode + "';";
            //string sql = "select * from ContractFile where projcode='" + projectCode + "'";
            //string sql= "declare @htbh varchar(50);set @htbh=(select contractcode from Contract where projcode='" + projectCode + "')";
            string sql = "select * from ContractPlan where projcode='" + projectCode + "' or projcode=(select top 1 contractcode from Contract where projcode='" + projectCode + "') order by skrq";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetContractPlanInfo(string id)
        {

            //string sql = "select * from Contract where projcode='" + projectCode + "'";
            //string sql = "select * from Assets  where projcode='" + projectCode + "';";
            //string sql = "select * from ContractFile where projcode='" + projectCode + "'";
            string sql = "select * from ContractPlan where ID='" + id + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        //public static async Task SavePlans(JArray jArray)
        //{
        //    string sql = "";
        //    if (ID == 0)
        //    {
        //        sql = "Insert into ContractPlan(projcode,bz,skrq,je,sqbl,nd,fkfs,zqs) values('" + pcode + "','" + bz + "','" + date + "'," + je + ",'" + bl + "','" + nd + "','" + fkfs + "','" + zqs + "');";
        //    }
        //    else
        //    {
        //        sql = "Update ContractPlan set bz='" + bz + "',projcode='" + pcode + "',skrq='" + date + "',je=" + je + ",sqbl='" + bl + "',nd='" + nd + "',fkfs='" + fkfs + "',zqs='" + zqs + "' where ID=" + ID;
        //    }
        //    await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        //}

        public static async Task SaveContractPlan( string pcode, string bz, DateTime date, decimal je, string bl, string nd, string fkfs, string zqs)
        {
            string sql = "";
            //if (ID == 0)
            //{
            //    sql = "Insert into ContractPlan(projcode,bz,skrq,je,sqbl,nd,fkfs,zqs) values('" + pcode + "','" + bz + "','" + date + "'," + je + ",'" + bl + "','" + nd + "','" + fkfs + "','" + zqs + "');";
            //}
            //else
            //{
                sql = "delete from ContractPlan where projcode='" + pcode + "' and convert(char(10),skrq,120)='"+date.ToString("yyyy-MM-dd")+"'";
                sql += "Insert into ContractPlan(projcode,bz,skrq,je,sqbl,nd,fkfs,zqs) values('" + pcode + "','" + bz + "','" + date + "'," + je + ",'" + bl + "','" + nd + "','" + fkfs + "','" + zqs + "');";
            //}
            sql += "delete from HT_SKJH where htbh='" + pcode + "';" +
                "declare @id int;set @id=isnull((select max(listid) from ht_skjh),0)+1;insert into HT_SKJH(listid,htbh,skfs,qs,jhskbl,jhskje,jhskrq,jhsknr,ssnd)  " +
                "select @id,projcode,fkfs,zqs,sqbl,je,skrq,bz,nd from ContractPlan where projcode='" + pcode + "';";
            //sql += "update HT_SKJH set jhskrq=null where jhskrq='1900-01-01 00:00:00.000' and htbh='" + ht.htbh + "' ; ";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task DeleteContractPlanByProjcode(string projcode)
        {

            string sql = "";
            sql = "delete from  ContractPlan where projcode='" + projcode + "'";

            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        public static async Task DeleteContractPlan(int ID)
        {

            string sql = "";
            sql = "delete from  ContractPlan where ID=" + ID;

            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task SaveMoney(ContractMoneyInfo info)
        {
            string sql = "";
            if (info.dzje > 0)
            {
                sql = "update ContractMoney set dzje=" + info.dzje + ",dzrq='" + info.dzrq + "',dzbz='" + info.dzbz + "' where skid='" + info.skid + "'";

            }
            else
            {
                sql = "insert into ContractMoney(planid,skid,projcode,htje,ysje,kpje,ysrq,kprq,kpbz) values(";
                sql += "'" + info.planid + "','" + info.skid + "','" + info.projcode + "'," + info.htje + "," + info.ysje + "," + info.kpje + ",";
                sql += "'" + info.ysrq + "','" + info.kprq + "','" + info.kpbz + "')";
            }
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task SaveProject(JObject j)
        {
            string sql = "begin delete from Project where projcode='" + j["projcode"] + "';";
            sql += "delete from AssetP where projcode='" + j["projcode"] + "';";
            sql += "insert into Project(projcode,trademode,projname,completiontime,projresult) values('" + j["projcode"].ToString() + "','" + j["trademode"] + "'," +
                "'" + j["projname"] + "','" + j["completiontime"] + "','0');";
            JArray ja = JArray.Parse(j["assets"].ToString());
            foreach (JObject o in ja)
            {
                sql += "insert into AssetP(projcode,assetcode,assetid,assetreserveprice,priceunit) values('" + j["projcode"].ToString() + "'," +
                    "'" + o["assetcode"] + "','" + o["assetid"] + "'," + Convert.ToDecimal(o["assetreserveprice"]) / 10000 + ",'" + o["priceunit"] + "');";
            }
            sql += " end";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task SaveProject1(JObject j)
        {
            JArray ja = JArray.Parse(j["assetids"].ToString());
            string sql = "begin delete from Projects where projcode='" + j["projcode"] + "';";
            foreach (string jb in ja)
            {
                sql += "insert into Projects(projcode,assetid,functioncode,remark) values('" + j["projcode"].ToString() + "','" + jb.ToString() + "'," +
                    "'" + j["functioncode"] + "','" + j["remark"] + "');";
            }
            sql += " end";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }

        public static async Task<DataTable> GetProject1()
        {
            string sql = "select distinct projcode,functioncode from Projects where projcode not in(select projcode from Contract)";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<(DataTable, int)> GetProject(string crf, string xmbh, string xmmc, string jylx, string cjzt, int Size, int page)
        {
            string sql = @"select * from (
select a.*,(select orgname from send where id =
(select top 1 SendID from AssetP a inner join SendAssets b on a.assetid = b.Assetid)) as crf,
(select orgname from send where id =
(select top 1 SendID from AssetP a inner join SendAssets b on a.assetid = b.Assetid)) as ssdw,
(case trademode when '1' then '招租' when '2' then '续租' when '3' then '转让' when '4' then '其他' when '5' then '定向招租' end) as trademodename,b.mc
from Project a left join Projresult b on a.projresult = b.bh) c where 1=1
 ";
            if (crf != "")
            {
                sql += " and crf like '%" + crf + "%'";
            }
            if (xmbh != "")
            {
                sql += " and projcode like '%" + xmbh + "%'";
            }
            if (xmmc != "")
            {
                sql += " and projname like '%" + xmmc + "%'";
            }
            if (jylx != "")
            {
                sql += " and trademodename like '%" + jylx + "%'";
            }
            if (cjzt != "")
            {
                sql += " and mc like '%" + cjzt + "%'";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, "projcode", true, Size, page);
        }

        public static async Task<DataTable> Getpro(string projcode)
        {
            string sql = @"select * from (
select a.*,(select orgname from send where id =
(select top 1 SendID from AssetP a inner join SendAssets b on a.assetid = b.Assetid)) as crf,
(select orgname from send where id =
(select top 1 SendID from AssetP a inner join SendAssets b on a.assetid = b.Assetid)) as ssdw,
(case trademode when '1' then '招租' when '2' then '续租' when '3' then '转让' when '4' then '其他' when '5' then '定向招租' end) as trademodename,b.mc
from Project a left join Projresult b on a.projresult = b.bh) c where projcode='" + projcode + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);


        }

        public static async Task<DataTable> GetProjresult()
        {
            string sql = "select * from Projresult";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetAssetP(string projcode)
        {
            string sql = @"select a.*,isnull(b.mj,0) as mj,isnull(b.zdj,0) as zdj,isnull( c.zcmc,'') as zcmj,isnull( (case when c.zclx='固定资产' then  c.zcxz else zylx end),'') as zclb,isnull(c.zclx,'') as zclx,isnull(jldw,'') as jldw,isnull(d.orgName,'') as ssdw,
isnull(e.assettransprice,0) as cjj from AssetP a left join SendAssets b on a.assetid=b.Assetid left join 
Asset c on a.assetcode=c.zcbm left join Org d on left(a.assetcode,15)=d.org left join Assets e on a.assetid=e.assetid
 where a.projcode='" + projcode + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task SaveIntendedtransferee(JObject j)
        {
            string sql = "update Project set transfereecount=" + Convert.ToInt32(j["transfereecount"]) + " where projcode='" + j["projcode"] + "';";

            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        public static async Task SaveProjectendresult(JObject j)
        {
            string sql = "update Project set projresult='" + Convert.ToInt32(j["projresult"]) + "',endtime='" + j["endtime"] + "' where projcode='" + j["projcode"] + "';";

            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        #endregion

        #region "资产概况"
        public static async Task<DataTable> GetGDZCPH(string org)
        {

            string sql = @"declare @c int;declare @org varchar(50);set @org=" + org + @"
            set @c = len(@org);  select x.org,x.orgname,isnull(jyx,0) as jyx,isnull(fjyx,0) as fjyx from Org x left join (     
            select org,orgname,sum(jyx) as jyx,sum(fjyx) as fjyx from 
(select b.org,b.orgName,isnull((case when  cyjg='经营性资产' then sum(yz) end),0)/10000 as jyx,
isnull((case when cyjg<>'经营性资产' then sum(yz) end),0)/10000 as fjyx
from asset a left join Org b on left(a.zcbm,@c+3)=b.Org where zclx='固定资产' and left(zcbm, @c)=@org
group by b.org,b.orgName,cyjg) c group by org,orgname) y on x.org=y.org where left(x.org, @c)= @org and x.isshow='1' and len(x.org)= @c + 3 ";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetJYGDZCPH(string org)
        {

            string sql = @"declare @c int;declare @org varchar(50);set @org=" + org + @"
            set @c = len(@org); select x.org,x.orgname,isnull(bl,0) as bl,isnull(yz,0) as yz from Org x left join (       
           select e.org,e.orgName,convert(decimal(18,2), (case when f.yz=0 then 0 else e.yz/f.yz*100 end)) as bl,e.yz/10000 as yz from (
(select org,orgname,COUNT(1) as xz,sum(yz) as yz from Asset a left join Org b on left(a.zcbm,@c+3)=b.Org where zclx='固定资产' and cyjg= '经营性资产'
 and zcbm not in (select assetcode from Assets c left join Contract d on c.projcode=d.projcode where contractenddate>GETDATE())
group by org,orgname) e inner join 
(select org,orgname,COUNT(1) as zs,sum(yz) as yz from Asset a left join Org b on left(a.zcbm,@c+3)=b.Org where zclx='固定资产' and cyjg= '经营性资产'
group by org,orgname) f on e.org=f.org) where left(e.org,@c)=@org) y on x.org=y.org where left(x.org, @c)= @org and x.isshow='1' and len(x.org)= @c + 3";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetGDZCQS(string org)
        {

            string sql = @"declare @c int;declare @org varchar(50);set @org=" + org + @"
            set @c = len(@org);       
           select convert(int, right(month,2)) as month,sum(jyx) as jyx,sum(fjyx) as fjyx from 
(select org,orgName,left(jgsj,6) as month, isnull((case when cyjg= '经营性资产'then sum(yz) end),0)/10000 as jyx,
isnull((case when cyjg<> '经营性资产'then sum(yz) end),0)/10000 as fjyx
from Asset a inner join Org b on left(a.zcbm,@c+3)=b.Org and b.isshow='1' where left(jgsj,4)=year(GETDATE()) and
zclx='固定资产' and left(zcbm, @c)=@org
group by org,orgname,cyjg,left(jgsj,6)) c group by convert(int, right(month,2))";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZYZCQS(string org)
        {

            string sql = @"declare @c int;declare @org varchar(50);set @org=" + org + @"
            set @c = len(@org);       
           select sum(jyx) as jyx,sum(fjyx) as fjyx,convert(int, right(month,2)) as month from 
(select org,orgName,left(jgsj,6) as month, isnull((case when cyjg= '经营性资产'then sum(zymj) end),0) as jyx,
isnull((case when cyjg<> '经营性资产'then sum(zymj) end),0) as fjyx
from Asset a inner join Org b on left(a.zcbm,@c+3)=b.Org and b.isshow='1' where left(jgsj,4)=year(GETDATE()) and
zclx<>'固定资产' and left(zcbm, @c)=@org
group by org,orgname,cyjg,left(jgsj,6)) c group by convert(int, right(month,2))";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        public static async Task<DataTable> GetZYZCSJ(string org)
        {

            string sql = @"declare @c int;declare @org varchar(50);set @org=" + org + @"
            set @c = len(@org); select x.org,x.orgname,isnull(jyx,0) as jyx,isnull(fjyx,0) as fjyx from Org x left join (      
           select org,orgname,sum(jyx) as jyx,sum(fjyx) as fjyx from 
(select org,orgName, isnull((case when cyjg = '经营性资产'then sum(zymj) end),0) as jyx,
isnull((case when cyjg<> '经营性资产'then sum(zymj) end),0) as fjyx
from Asset a inner join Org b on left(a.zcbm,@c+3)=b.Org where zclx<>'固定资产' and left(zcbm, @c)=@org
group by org,orgname,cyjg) c group by org,orgname)  y on x.org=y.org where left(x.org, @c)= @org and x.isshow='1' and len(x.org)= @c + 3";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZYZCPH(string org)
        {

            string sql = @"declare @c int;declare @org varchar(50);set @org=" + org + @"
            set @c = len(@org); select x.org,x.orgname,isnull(bl,0) as bl,isnull(mj,0) as mj from Org x left join (       
           select e.org,e.orgName, convert(decimal(18,2), (case when f.mj=0 then 0 else e.mj/f.mj*100 end)) as bl,e.mj from (
(select org,orgname,COUNT(1) as xz,sum(zymj) as mj from Asset a left join Org b on left(a.zcbm,@c+3)=b.Org where zclb<>'固定资产' and cyjg= '经营性资产'
 and zcbm not in (select assetcode from Assets c left join Contract d on c.projcode=d.projcode where contractenddate>GETDATE())
group by org,orgname) e inner join 
(select org,orgname,COUNT(1) as zs,sum(zymj) as mj  from Asset a left join Org b on left(a.zcbm,@c+3)=b.Org where zclx<>'固定资产' and cyjg = '经营性资产'
group by org,orgname) f on e.org=f.org) where left(e.org,@c)=@org) y on x.org=y.org where left(x.org, @c)= @org and x.isshow='1' and len(x.org)= @c + 3";

            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }


        #endregion



        #region "数据校验"
        public static async Task<DataTable> CheckData(string month, string org)
        {
            string sql = @"declare @month varchar(6);declare @org varchar(50);declare @tablename varchar(50);
set @tablename='##temp'+REPLACE(NEWID(),'-','');
set @month='" + month + @"';set @org='" + org + @"';
 declare @c int;
  set @c=LEN(@org);
  declare @sql varchar(8000);
  set @sql='create table '+@tablename+'(id int,xh int,ly varchar(100),xm varchar(500),hj decimal(23,2)';
  
  declare @o varchar(500);declare @on varchar(500);
  declare ColCur cursor for select distinct org,orgname From Org where left(org,@c)=@org and len(org)=@c+3 and isshow='1' order by org,orgname;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o,@on;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN  
     SET @sql=@sql+','+@on+' decimal(18,2)';
          FETCH NEXT FROM ColCur INTO @o,@on;  
   END  
close ColCur;  
deallocate ColCur;  
set @sql=@sql+')';
print @sql;
exec(@sql);

set @sql='';
set @sql='insert into '+@tablename+' (id,xh,ly,xm) select id,xh,ly,xm from CheckData;';
print @sql;
exec(@sql);

set @sql='';
 declare @o1 varchar(500);declare @on1 varchar(500);
  declare ColCur cursor for select distinct org,orgname From Org where left(org,@c)=@org and len(org)=@c+3 and isshow='1' order by org,orgname;  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o1,@on1;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN 
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(INCOME_TOTAL)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''1'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(OPERATING_TOTAL)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''2'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(AWARDING_TOTAL)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''3'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(GRANT_TOTAL)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''4'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(OTHER_TOTAL)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''5'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(INVEST_TOTAL)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''6'');'
exec(@sql);
set @sql='';
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(CREDIT_PROFIT_TOTAL)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''7'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(OPERATING_INCOME)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''8'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(AWARDING_INCOME)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''9'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(FARM_TAXED_RETURN)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''10'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(OTHER_INCOME)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''11'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(INVESTMENT_INCOME)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''12'');'
exec(@sql);
set @sql='';
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(SPENDING_TOTAL)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''13'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(BUSINESS_SPENDING)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''14'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(MANAGEMENT_TOTAL)/10000 from szmxb where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''15'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(BORROWING_PROFIT_TOTAL)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''16'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(BUSINESS_SPENDING)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''17'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(TAXES_APPEND_SPENDING)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''18'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(MANAGEMENT_TOTAL)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''19'');'
exec(@sql);
set @sql='';
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(PUBLIC_SPENDING)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''20'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(FARM_DEVELOP_SPENDING)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''21'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(OTHER_SPENDING)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''22'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(ASSETS_TOTAL)/10000 from ZCFZB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''23'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(LIABILITIES_OWNER_SUM)/10000 from ZCFZB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''24'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(LIABILITIES_TOTAL+OWNER_TOTAL)/10000 from ZCFZB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''25'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(LIABILITIES_OWNER_SUM)/10000 from ZCFZB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''26'');'
exec(@sql);
set @sql='';
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(BORROWING_ASSETS_TOTAL)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''27'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(CREDIT_LIABILITIES_TOTAL+CREDIT_OWNER_TOTAL+CREDIT_PROFIT_TOTAL-BORROWING_PROFIT_TOTAL)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''28'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(ACCOUNTS_RECEIVABLE)/10000 from ZWZQB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''29'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(ACCOUNTS_RECEIVABLE)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''30'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(CURRENT_ACCOUNTS_RECEIVABLE)/10000 from ZWZQB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''31'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(ACCOUNTS_PAYABLE)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''32'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(PAYABLE_SALARY)/10000 from ZWZQB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''33'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(PAYABLE_SALARY)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''34'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(SHORT_TERM_BORROWING)/10000 from ZWZQB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''35'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(SHORT_TERM_BORROWING)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''36'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(LONG_TERM_BORROWING)/10000 from ZWZQB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''37'');'
set @sql=@sql+'update '+@tablename+' set '+@on1+'=(select sum(LONG_BORROWINGS)/10000 from KMYEB where ISSUE='''+@month+''' and left(AREACODE,'+CONVERT(varchar(6), @c+3)+')='''+@o1+''') where id= CONVERT(varchar(6),''38'');'
print @sql;
exec(@sql);
set @sql='';
         FETCH NEXT FROM ColCur INTO @o1,@on1;  
   END  
close ColCur;  
deallocate ColCur;  

set @sql='update '+@tablename+' set hj='
  declare @o2 varchar(500);
  declare ColCur cursor for select distinct orgname From Org where left(org,@c)=@org and len(org)=@c+3 and isshow='1';  
open ColCur;  
FETCH NEXT FROM ColCur INTO @o2;  
WHILE (@@FETCH_STATUS = 0)  
   BEGIN 
 set @sql=@sql+'isnull('+@o2+',0)'+'+';

         FETCH NEXT FROM ColCur INTO @o2;  
   END  
close ColCur;  
deallocate ColCur;  
declare @n int;
set @n=len(@sql);
set @sql=LEFT(@sql,@n-1);
print @sql;
exec(@sql);
set @sql='select * from '+@tablename+';drop table '+@tablename+';';
exec(@sql);";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion

        #region "发送资产给产交中心"
        public static async Task<bool> CheckID(string id)
        {
            string sql = "select * from SendAssets where Assetid like '"+id.Substring(0,id.LastIndexOf("-"))+"%'";
            DataTable table =await new RepositoryFactory().BaseRepository().FindTable(sql);
            if (table.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static async Task<string> SaveSend(JObject str)
        {
            string id = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            try
            {
                string sql = "insert into Send(ID,orgname ,areacode,areaname,bankname,bankno,mobilephone,dutyperson,contractperson,CreateDate,status,zdrid,zdr,fileUpload,acctnumb) values(";
                sql += "'" + id + "','" + GetString(str, "orgname") + "','" + GetString(str, "areacode") + "','" + GetString(str, "areaname") + "'," +
                    "'" + GetString(str, "bankname") + "','" + GetString(str, "yhhh") + "','" + GetString(str, "mobilephone") + "'," +
                    "'" + GetString(str, "dutyperson") + "','" + GetString(str, "contractperson") + "','" + DateTime.Today.ToString("yyyy-MM-dd") + "','-1','" + GetString(str, "zdrid") + "','" + GetString(str, "zdr") + "','" + GetString(str, "fileUpload") + "','" + GetString(str, "bankno") + "');";
                //await new RepositoryFactory().BaseRepository().ExecuteSql(sql,null);

                JArray ja = JArray.Parse(str["assets"].ToString());
                int x = 0;
                foreach (JObject jb in ja)
                {
                    string mj = GetString(jb, "mj") == "" ? "0" : GetString(jb, "mj");
                    string zdj = GetString(jb, "zdj") == "" ? "0" : GetString(jb, "zdj");
                    string zcbm = GetString(jb, "zcbm");
                    string assetsid = GetString(jb, "assetsid");
                    if (zcbm == "")
                    {
                        zcbm = GetString(jb, "assetcode");
                    }
                    if (assetsid == "")
                    {
                        assetsid = DateTime.Now.ToString("yyyyMMddHHmmssff") + x;
                        x++;
                    }
                    if (assetsid.Contains("-"))
                    {
                        if (await CheckID(assetsid))
                        {
                            assetsid = DateTime.Now.ToString("yyyyMMddHHmmssff") + x + "-" + zcbm;
                            x++;
                        }
                    }
                    
                    sql += "insert into SendAssets(SendID,zcbm,mj,jldw,zdj,jgdw,Assetid) values(" + id + ",'" + zcbm + "'," + Convert.ToDecimal(mj) + ",'" + GetString(jb, "jldw") + "'," + Convert.ToDecimal(zdj) + ",'" + GetString(jb, "jgdw") + "','" + assetsid + "');";
                    if (jb.Property("files") != null)
                    {
                        sql += "delete from SFile where sendid='" + GetString(str, "id") + "' and zcbm='" + zcbm + "'";
                        if (jb["files"].ToString() != "")
                        {
                            JArray jr = JArray.Parse(jb["files"].ToString());
                            foreach (JObject jt in jr)
                            {
                                if (GetString(jt, "projfiletype") == "附件" || GetString(jt, "projfiletype") == "")
                                {
                                    sql += "insert into SFile(sendid,zcbm,filename,fileurl) values('" + id + "','" + zcbm + "','" + jt["filename"] + "','" + jt["fileurl"] + "');";
                                }
                                else
                                {
                                    string projfiletype = GetString(jt, "projfiletype");
                                    string protype = "";
                                    switch (projfiletype)
                                    {
                                        case "立项申请书":
                                            protype = "uploadFile_LXSQB";
                                            break;
                                        case "交易方案":
                                            protype = "uploadFile_JYFA";
                                            break;
                                        case "表决书":
                                            protype = "uploadFile_BJS";
                                            break;
                                        case "会议决议":
                                            protype = "uploadFile_HYJY";
                                            break;
                                        case "公示情况报告表":
                                            protype = "uploadFile_QSZM";
                                            break;
                                    }
                                    sql += "insert into SFile (sendid,zcbm,filename,fileurl,projfiletype) values('" + id + "','" + zcbm + "','" + jt["filename"] + "','" + jt["fileurl"] + "','" + protype + "');";
                                }

                            }
                        }
                    }
                    //if (jb.Property("projattachments") != null)
                    //{
                    //    sql += "delete from SFile where sendid='" + GetString(str, "id") + "' and zcbm='" + zcbm + "' projfiletype is not null;";
                    //    if (jb["projattachments"].ToString() != "")
                    //    {
                    //        JArray jr = JArray.Parse(jb["projattachments"].ToString());
                    //        foreach (JObject jt in jr)
                    //        {
                    //            sql += "insert into SFile (sendid,zcbm,filename,fileurl,projfiletype) values('" + id + "','" + zcbm + "','" + jt["filename"] + "','" + jt["fileurl"] + "','"+ jt["projfiletype"] + "');";
                    //        }
                    //    }
                    //}

                }
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //sql = "select zcbm, right('000000000000'+rtrim(convert(char(20),Assetid)),10) as Assetid,mj from SendAssets where SendID=" + id;
            //return await new RepositoryFactory().BaseRepository().FindTable(sql,null);
            return id;
        }

        public static async Task<string> SaveSendx(JObject str)
        {
            string id = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            try
            {
                JArray ja = JArray.Parse(str["assets"].ToString());
                string areacode = ja[0]["orgid"].ToString();
                string sql = "insert into Send(ID,orgname ,areacode,areaname,bankname,bankno,mobilephone,dutyperson,contractperson,CreateDate,status,zdrid,zdr,fileUpload,acctnumb) values(";
                sql += "'" + id + "','" + GetString(str, "acctname") + "','" + areacode + "','" + GetString(str, "acctname") + "'," +
                    "'" + GetString(str, "bankname") + "','" + GetString(str, "bankno") + "','" + GetString(str, "mobilephone") + "'," +
                    "'" + GetString(str, "dutyperson") + "','" + GetString(str, "contractperson") + "','" + GetString(str, "zdrq") + "','2','" + GetString(str, "zdrid") + "','" + GetString(str, "zdr") + "','" + GetString(str, "fileUpload") + "','" + GetString(str, "acctnumb") + "');";
                //await new RepositoryFactory().BaseRepository().ExecuteSql(sql,null);


                foreach (JObject jb in ja)
                {
                    string mj = GetString(jb, "tradearea") == "" ? "0" : GetString(jb, "tradearea");
                    string zdj = GetString(jb, "zdj") == "" ? "0" : GetString(jb, "zdj");
                    string zcbm = GetString(jb, "zcbm");
                    if (zcbm == "")
                    {
                        zcbm = GetString(jb, "assetcode");
                    }
                    sql += "insert into SendAssets(SendID,zcbm,mj,jldw,zdj,jgdw,Assetid) values(" + id + ",'" + zcbm + "'," + Convert.ToDecimal(mj) + ",'" + GetString(jb, "tradeunit") + "'," + Convert.ToDecimal(zdj) + ",'" + GetString(jb, "tradeunit") + "'," + GetString(jb, "assetid") + ");";
                    if (jb.Property("attachments") != null)
                    {
                        sql += "delete from SFile where sendid='" + GetString(str, "id") + "' and zcbm='" + zcbm + "' and projfiletype is null;";
                        if (jb["attachments"].ToString() != "")
                        {
                            JArray jr = JArray.Parse(jb["attachments"].ToString());
                            foreach (JObject jt in jr)
                            {
                                sql += "insert into SFile(sendid,zcbm,filename,fileurl) values('" + id + "','" + zcbm + "','" + jt["filename"] + "','" + jt["fileurl"] + "');";
                            }
                        }
                    }
                    if (jb.Property("projattachments") != null)
                    {
                        sql += "delete from SFile where sendid='" + GetString(str, "id") + "' and zcbm='" + zcbm + "' and projfiletype is not null;";
                        if (jb["projattachments"].ToString() != "")
                        {
                            JArray jr = JArray.Parse(jb["projattachments"].ToString());
                            foreach (JObject jt in jr)
                            {
                                sql += "insert into SFile (sendid,zcbm,filename,fileurl,projfiletype) values('" + id + "','" + zcbm + "','" + jt["filename"] + "','" + jt["fileurl"] + "','" + jt["projfiletype"] + "');";
                            }
                        }
                    }
                }
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //sql = "select zcbm, right('000000000000'+rtrim(convert(char(20),Assetid)),10) as Assetid,mj from SendAssets where SendID=" + id;
            //return await new RepositoryFactory().BaseRepository().FindTable(sql,null);
            return id;
        }

        public static async Task UpdateSend(JObject str, string zt)
        {

            try
            {
                string sql = "update Send set orgname='" + GetString(str, "orgname") + "' ,areacode='" + GetString(str, "areacode")
                    + "',areaname='" + GetString(str, "areaname") + "',bankname='" + GetString(str, "bankname") + "',bankno='" + GetString(str, "yhhh")
                    + "',mobilephone='" + GetString(str, "mobilephone") + "',dutyperson='" + GetString(str, "dutyperson")
                    + "',contractperson='" + GetString(str, "contractperson") + "',status='" + zt + "',zdr='" + GetString(str, "zdr") + "',zdrid='" + GetString(str, "zdrid") + "',fileUpload='" + GetString(str, "fileUpload") + "',acctnumb='" + GetString(str, "bankno") + "' where id='" + GetString(str, "id") + "'";
                sql += "delete from SendAssets where SendID='" + GetString(str, "id") + "'";
                //await new RepositoryFactory().BaseRepository().ExecuteSql(sql,null);

                JArray ja = JArray.Parse(str["assets"].ToString());
                int x = 0;
                foreach (JObject jb in ja)
                {
                    string mj = GetString(jb, "mj") == "" ? "0" : GetString(jb, "mj");
                    string zdj = GetString(jb, "zdj") == "" ? "0" : GetString(jb, "zdj");
                    string zcbm = GetString(jb, "zcbm");
                    string assetsid = GetString(jb, "assetsid");
                    if (assetsid == "")
                    {
                        assetsid = DateTime.Now.ToString("yyyyMMddHHmmssff") + x;
                        x++;
                    }
                    if (zcbm == "")
                    {
                        zcbm = GetString(jb, "assetcode");
                    }
                    sql += "insert into SendAssets(SendID,zcbm,mj,jldw,zdj,jgdw,Assetid) values(" + GetString(str, "id") + ",'" + zcbm + "'," + Convert.ToDecimal(mj) + ",'" + GetString(jb, "jldw") + "'," + Convert.ToDecimal(zdj) + ",'" + GetString(jb, "jgdw") + "','" + assetsid + "');";
                    if (jb.Property("files") != null)
                    {
                        sql += "delete from SFile where sendid='" + GetString(str, "id") + "' and zcbm='" + zcbm + "'";
                        if (jb["files"].ToString() != "")
                        {
                            JArray jr = JArray.Parse(jb["files"].ToString());
                            foreach (JObject jt in jr)
                            {
                                if (GetString(jt, "projfiletype") == "附件" || GetString(jt, "projfiletype") == "")
                                {
                                    sql += "insert into SFile(sendid,zcbm,filename,fileurl) values('" + GetString(str, "id") + "','" + zcbm + "','" + jt["filename"] + "','" + jt["fileurl"] + "');";
                                }
                                else
                                {
                                    string projfiletype = GetString(jt, "projfiletype");
                                    string protype = "";
                                    switch (projfiletype)
                                    {
                                        case "立项申请书":
                                            protype = "uploadFile_LXSQB";
                                            break;
                                        case "交易方案":
                                            protype = "uploadFile_JYFA";
                                            break;
                                        case "表决书":
                                            protype = "uploadFile_BJS";
                                            break;
                                        case "会议决议":
                                            protype = "uploadFile_HYJY";
                                            break;
                                        case "公示情况报告表":
                                            protype = "uploadFile_QSZM";
                                            break;
                                    }
                                    sql += "insert into SFile (sendid,zcbm,filename,fileurl,projfiletype) values('" + GetString(str, "id") + "','" + zcbm + "','" + jt["filename"] + "','" + jt["fileurl"] + "','" + protype + "');";
                                }

                            }
                        }
                    }


                    //if (jb.Property("projattachments") != null)
                    //{
                    //    sql += "delete from SFile where sendid='" + GetString(str, "id") + "' and zcbm='" + zcbm + "' projfiletype is not null;";
                    //    if (jb["projattachments"].ToString() != "")
                    //    {
                    //        JArray jr = JArray.Parse(jb["projattachments"].ToString());
                    //        foreach (JObject jt in jr)
                    //        {
                    //            sql += "insert into SFile (sendid,zcbm,filename,fileurl,projfiletype) values('" + GetString(str, "id") + "','" + zcbm + "','" + jt["filename"] + "','" + jt["fileurl"] + "','" + jt["projfiletype"] + "');";
                    //        }
                    //    }
                    //}
                }
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //sql = "select zcbm, right('000000000000'+rtrim(convert(char(20),Assetid)),10) as Assetid,mj from SendAssets where SendID=" + id;
            //return await new RepositoryFactory().BaseRepository().FindTable(sql,null);

        }

        public static async Task UpdateSend(string id, string zt)
        {
            string sql = "update Send set status='" + zt + "' where id='" + id + "'";
            sql += "Update SendAssets set Assetid=left(SendID,17)+'-'+zcbm where SendID='" + id + "' and Assetid=''";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
        }

        public static async Task<DataTable> GetSend(string id)
        {
            string sql = "select ID,orgname,areacode,areaname,bankname,bankno as yhhh,mobilephone,dutyperson,contractperson,CreateDate,status,zdrid,zdr,fileUpload,acctnumb as bankno from Send where id='" + id + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetSendByOrg(string org)
        {
            string sql = @"select top 1 areacode,areaname,bankname,bankno as yhhh,mobilephone,dutyperson,contractperson,acctnumb as bankno 
from Send where areacode = '" + org + "' order by CreateDate desc";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetSendAssets(string id)
        {

            string sql = @"select distinct zcbm,Assetid,Assetid as aid,mj,zdj,jgdw,c.JZMJ-ISNULL(b.yy,0) as kymj,(case when left(lbdm,2)='20' then '2' else '1' end) as assetclass,
(case lbdm when '1001' then '65' when '1002' then '66' when '1003' then '67' when '1004' then '67' when '1005' then '67' when '1006' then '67' when '1007' then '67' when '1008' then '67'
 when '200106' then '74' when '200101' then '74' when '200102' then '74' when '200103' then '74' when '200104' then '74' when '200105' then '74'  when '200107' then '74' when '200108' then '74'
  when '200201' then '73' when '200202' then '73' when '200203' then '73' when '200204' then '73' when '200205' then '73' when '200206' then '73' when '200601' then '69'
  when '200602' then '69' when '3001' then '70' when '3002' then '70' when '5001' then '71' when '5002' then '71' when '5003' then '71' end) as assettype,
  (case lbdm when '200101' then '292' when '200102' then '293' when '200103' then '294' when '200104' then '295' when '200105' then '296' when '200106' then '297'
  when '200107' then '298' when '200201' then '299' when '200202' then '300' when '200206' then '324'
  when '200203' then '325' when '200204' then '326' when '200205' then '327' when '200501' then '317' when '200502' then '329' when '2004' then '330'
  when '200503' then '332' when '200601' then '333' when '200602' then '334' else '0' end) as resourcetype,zlwz as assetsite,JZMJ as  tradearea,(case when GDDW= '平方米' then '2' else '1' end)  as tradeunit,left(gddm, 12) as orgid,BMMC,GDDW
   from SendAssets a left join 
(select c.assetcode,sum(tradearea) as yy from Assets c left join Contract d on c.projcode=d.projcode where contractenddate>GETDATE() group by c.assetcode) b 
on a.zcbm=b.assetcode left join vw_fa_kp c on a.zcbm=c.GDDM where SendID='" + id + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);

        }

        public static async Task<string> DeleteSend(string id)
        {
            string ver = "";
            try
            {
                string sql = "delete from Send where id='" + id + "';delete from SendAssets where SendID='" + id + "'";
                await new RepositoryFactory().BaseRepository().ExecuteSql(sql, null);
            }
            catch (Exception ex)
            {
                ver = ex.Message;
            }
            return ver;
        }

        public static async Task<DataTable> GetAssetFile(string id, string zcbm)
        {
            string sql = "select * from SFile where sendid='" + id + "' and zcbm='" + zcbm + "' and projfiletype is null";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetAssetFiles(string id, string zcbm)
        {
            string sql = "select * from SFile where sendid='" + id + "' and zcbm='" + zcbm + "' and projfiletype is not null";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<(IEnumerable<dynamic>, int)> GetSend(string org, string crf, string zcbm, string zcmc, string start, string end, string zt, int page, int Size)
        {
            string sql = @"select a.id,a.areaname,(case when isnull(a.createDate,'')='' then CONVERT(varchar(10), CONVERT(datetime,LEFT(id,8)),120) else a.CreateDate end) as createDate,status,a.zdr,
(case  when status='2' and isnull(b.projresult,'')='' and isnull(c.projresult,'')=''  then '已发送' when isnull(b.projresult,'')='0' and isnull(c.projresult,'')='' then '项目立项' 
when isnull(b.projresult,'')='12' and isnull(c.projresult,'')='' then '合同录入'  when isnull(b.projresult,'')='' and isnull(c.projresult,'')='0' then '项目立项' 
when isnull(b.projresult,'')='' and isnull(c.projresult,'')='12' then '合同录入' else '未立项' end ) as projresult,
(case when b.projcode is null then c.projcode else b.projcode end) as projcode
from Send a left join
(select distinct f.projcode,f.projresult,a.SendID from SendAssets a  left join (select assetid,e.projresult,d.projcode from  
AssetP d inner join Project e on d.projcode=e.projcode) f on a.assetid='096'+SUBSTRING(a.assetid,0,CHARINDEX('-',a.assetid)) and left(a.Assetid,3)<>'096') b on a.ID=b.SendID
left join
(select distinct f.projcode,f.projresult,a.SendID from SendAssets a  left join (select assetid,e.projresult,d.projcode from  
AssetP d inner join Project e on d.projcode=e.projcode) f on a.assetid=f.assetid and left(a.Assetid,3)='096') c on a.ID=c.SendID
  where a.areacode like '" + org + "%'";
            if (crf != "")
            {
                sql += " and a.areaname like '%" + crf + "%'";
            }
            if (!string.IsNullOrEmpty(zcbm))
            {
                sql += " and id in(select SendID from SendAssets where zcbm like '%" + zcbm + "%')";
            }
            if (!string.IsNullOrEmpty(zcmc))
            {
                sql += " and id in(select SendID from SendAssets x inner join  asset y on x.zcbm=y.zcbm where y.zcmc like '%" + zcmc + "%')";
            }
            if (start != "" && end != "" && Convert.ToDateTime(end) >= Convert.ToDateTime(start))
            {
                sql += " and (a.CreateDate>='" + start + "' and a.CreateDate<='" + end + "')";
            }
            if (zt != "")
            {
                sql += " and status = '" + zt + "'";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, "ID", false, Size, page);
        }

        public static async Task<DataTable> GetAssets(string assetid, string projcode)
        {
            string sql = "select * from SendAssets where Assetid like '%" + assetid.Substring(3, assetid.Length - 3) + "%' and left(zcbm,9)='320571050'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        #endregion
        #region "产交接口"
        public static async Task<DataTable> projectpublish(string projcode)
        {
            string sql = "select * from Project where projcode='" + projcode + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> projectpublishAssets(string projcode)
        {
            string sql = "select projcode ,assetcode ,'100'+SUBSTRING(assetid,4,len(rtrim(assetid))-3) as assetid ,assetreserveprice,priceunit from AssetP where projcode='" + projcode + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion


        #region "资产负债表导入"

        public static async Task deleteDR(string kjqj, string org)
        {
            string sql = "delete from ZCFZBDR where kjqj='"+kjqj+"' and org='"+org+"'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        public static async Task SaveDR(List<ZCFZBDR> dr)
        {
            string org = "";
            foreach (ZCFZBDR d in dr)
            {
                await new RepositoryFactory().BaseRepository().Insert(d);
                org = d.org;
            }
            string sql = "Update ZCFZBDR set org='" + org.Substring(0, 9) + "' where isnull(org,'')='';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }



        public static async Task SaveFileDR(ImportFile f)
        {
            await deletefile(f);
            await new RepositoryFactory().BaseRepository().Insert(f);

        }

        public static async Task deletefile(ImportFile f)
        {
            string sql = "delete from ImportFile where org='" + f.org + "' and kjqj='" + f.kjqj + "';";
            sql += "delete from ZCFZBDR where org like'" + f.org + "%' and kjqj='" + f.kjqj + "';";
            await new RepositoryFactory().BaseRepository().ExecuteSql(sql);
        }
        public static async Task<DataTable> GetFiles(string org, string kjqj)
        {
            string sql = "select * from ImportFile where org='" + org + "' and kjqj='" + kjqj + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetZCFZBDR(string org, string kjqj)
        {
            string sql = @"declare @org varchar(50);
declare @kjqj varchar(6);
declare @tablename varchar(200);
set @tablename='##temp'+REPLACE(NEWID(),'-','');
set @org='" + org + @"';
set @kjqj='" + kjqj + @"';
 declare @c int;
 set @c=len(@org);
 if(@c=9)
 begin
 declare @sql nvarchar(max);
 set @sql='create table '+@tablename+'(xh varchar(50),项目 varchar(50),剔除前合计 varchar(50),剔除合计 varchar(50),剔除后合计 varchar(50))';
  declare @s1 nvarchar(max);
set @s1='';
  declare @s2 nvarchar(max);
set @s2='';
 declare @sqli nvarchar(max);
 set @sqli='insert into '+@tablename+' (xh,项目,剔除前合计,剔除合计,剔除后合计) select a.xh,a.xm,b.tcq,tc,tch from TB_TableSet a left join ZCFZBDR b 
 on a.xh=b.xh and org='''+@org+''' where lx=''8''  and  orgname<>''''  and kjqj='''+@kjqj+'''';
 exec(@sql);
print @sqli;
exec (@sqli);
  declare @o varchar(500);
 declare @gx varchar(500);
 declare @cn int;
 set @cn=0;
declare ColCur cursor for select distinct org,orgname from ZCFZBDR where left(org,9)=@org and len(org)=15 and kjqj=@kjqj
open ColCur;  
 FETCH NEXT FROM ColCur INTO @o,@gx;  
WHILE (@@FETCH_STATUS = 0)  
BEGIN 
set @gx=REPLACE(@gx,'（','');
set @gx=REPLACE(@gx,'）','');
SET @sql='alter table  '+@tablename+' add '+@gx+'剔除前 varchar(50)';
SET @sql=@sql+'alter table  '+@tablename+' add '+@gx+'剔除 varchar(50)';
SET @sql=@sql+'alter table  '+@tablename+' add '+@gx+'剔除后 varchar(50)';
print @sql;
 exec(@sql)

set  @sqli='Update '+@tablename+' set '+@gx+'剔除前=(select tcq from ZCFZBDR  
where org='''+@o+''' and kjqj='''+@kjqj+'''  and  orgname<>''''   and xh='+@tablename+'.xh)';
set  @sqli= @sqli+'Update '+@tablename+' set '+@gx+'剔除=(select tc from ZCFZBDR  
where org='''+@o+'''  and kjqj='''+@kjqj+'''  and  orgname<>''''  and xh='+@tablename+'.xh)';
set  @sqli= @sqli+'Update '+@tablename+' set '+@gx+'剔除后=(select tch from ZCFZBDR  
where org='''+@o+''' and kjqj='''+@kjqj+'''  and  orgname<>''''  and xh='+@tablename+'.xh)';
print @sqli;
exec (@sqli);
FETCH NEXT FROM ColCur INTO @o,@gx;  
END  
close ColCur;  
deallocate ColCur;  
 --set @sql=@sql+@s1+')';
-- print @sql;
-- print @s1;
-- print @s2;
--print @sql+@s1+@s2+')';
----print @s1;
--exec(@sql+@s1+@s2+')');
--print @sqli;
--exec (@sqli);
 set @sql='select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
exec(@sql);
end
else
begin

declare @sql1 nvarchar(max);
 set @sql1='create table '+@tablename+'(xh varchar(50),项目 varchar(50),剔除前合计 varchar(50),剔除合计 varchar(50),剔除后合计 varchar(50)';
 declare @sqli1 nvarchar(max);
 set @sqli1='insert into '+@tablename+' (xh,项目,剔除前合计,剔除合计,剔除后合计) select a.xh,a.xm,CONVERT(varchar(50), sum(convert(decimal(18,2),(case when tcq='''' then ''0'' else REPLACE(REPLACE(tcq,''('',''-''),'')'','''') end)))),
CONVERT(varchar(50),sum(convert(decimal(18,2),(case when tc='''' then ''0'' else REPLACE(REPLACE(tc,''('',''-''),'')'','''') end)))),
CONVERT(varchar(50),sum(convert(decimal(18,2),(case when tch='''' then ''0'' else REPLACE(REPLACE(tch,''('',''-''),'')'','''') end))))  from TB_TableSet a left join ZCFZBDR b 
 on a.xh=b.xh and org like'''+@org+'%'' and len(org)=9 where lx=''8''  and  orgname<>'''' and kjqj='''+@kjqj+''' group by a.xh,a.xm ';

 declare @o1 varchar(500);
 declare @gx1 varchar(500);
declare ColCur cursor for select distinct org,orgname from Org where porg=@org and len(org)=9
open ColCur;  
 FETCH NEXT FROM ColCur INTO @o1,@gx1;  
WHILE (@@FETCH_STATUS = 0)  
BEGIN  
SET @sql1=@sql1+','+@gx1+'剔除前 varchar(50)';
SET @sql1=@sql1+','+@gx1+'剔除 varchar(50)';
SET @sql1=@sql1+','+@gx1+'剔除后 varchar(50)';
set  @sqli1= @sqli1+'Update '+@tablename+' set '+@gx1+'剔除前=(select tcq from ZCFZBDR  
where org like '''+@o1+''' and len(org)=9 and kjqj='''+@kjqj+'''  and  orgname<>''''  and xh='+@tablename+'.xh)';
set  @sqli1= @sqli1+'Update '+@tablename+' set '+@gx1+'剔除=(select tc from ZCFZBDR  
where org like '''+@o1+''' and len(org)=9 and kjqj='''+@kjqj+'''  and  orgname<>''''  and xh='+@tablename+'.xh)';
set  @sqli1= @sqli1+'Update '+@tablename+' set '+@gx1+'剔除后=(select tch from ZCFZBDR  
where org like '''+@o1+''' and len(org)=9 and kjqj='''+@kjqj+'''   and  orgname<>'''' and xh='+@tablename+'.xh)';
FETCH NEXT FROM ColCur INTO @o1,@gx1;  
END  
close ColCur;  
deallocate ColCur;  
 set @sql1=@sql1+')';
print @sql1;
exec(@sql1);


print @sqli1;
exec (@sqli1);
 set @sql1='select * from '+@tablename+' order by convert(int, xh);drop table '+@tablename+';';
exec(@sql1);

end
";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> Getkjqj()
        {
            string sql = "select distinct left(kjqj,4)+'-'+RIGHT(kjqj,2) as kjqj from ImportFile order by kjqj desc";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }

        public static async Task<DataTable> GetMaxkjqj()
        {
            string sql = "select max(kjqj) as kjqj from ImportFile";
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null);
        }
        #endregion
    }
}
